【问题标题】:Dynamically created TextBox Name and Position Issue动态创建的文本框名称和位置问题
【发布时间】:2013-12-23 19:37:39
【问题描述】:

我已经完成了我提出的问题,感谢所有回答的人。

目前在我的应用程序中,我有两个按钮可以创建我的文本框并将它们放在我希望它们去的位置。

C#代码:

private void btnAddTitle_Click(object sender, RoutedEventArgs e)
{
        TextBox x = new TextBox();
        x.Name = "new_textbox";
        x.TextWrapping = TextWrapping.Wrap;
        x.Height = 25;
        x.Width = 200;
        x.AcceptsReturn = true;
        x.Margin = new Thickness(10, 15, 950, 0);
        spStandard.Children.Add(x);
 }

 private void btnQuestion_Click(object sender, RoutedEventArgs e)
 {
        TextBox x = new TextBox();
        x.Name = "new_textbox";
        x.TextWrapping = TextWrapping.Wrap;
        x.Height = 25;
        x.Width = 200;
        x.AcceptsReturn = true;
        x.Margin = new Thickness(10, 15, 850, 0);
        spStandard.Children.Add(x);
 }

XAML 代码:

<Button x:Name="btnAddTitle" Content="Add Title" HorizontalAlignment="Left" Margin="919,30,0,0" VerticalAlignment="Top" Width="121" Height="24" Background="{x:Null}" Click="btnAddTitle_Click"/>
<Button x:Name="btnQuestion" Content="Add Question" HorizontalAlignment="Left" Margin="1080,30,0,0" VerticalAlignment="Top" Width="121" Height="24" Click="btnQuestion_Click"/>

<Rectangle Fill="#FFF4F4F5" HorizontalAlignment="Left" Height="1" Margin="22,82,0,0" Stroke="Black" VerticalAlignment="Top" Width="1200"/>
<Border CornerRadius="6" BorderBrush="Black" BorderThickness="2" Margin="34,132,33,72">
        <StackPanel x:Name="spStandard" HorizontalAlignment="Left" Margin="0,-2,-2,-2" Width="1181"/>
</Border>

运行中的代码图片:

http://i.stack.imgur.com/REWTe.png

(Title TextBoxs更靠近边框,问题TextBoxs有间隙)

回答我的第一个问题是:当我点击按钮时,它会动态创建不同的文本框。我怎样才能给他们不同的名称/ID,以便我以后需要时可以从该文本框中获取信息?

我的最后一个问题是:当我编辑 TextBox (x.Width = 200;) 的宽度以便用户可以添加更大的问题时,TextBox 会丢失位置和边距。

图片:

http://i.stack.imgur.com/JKhUH.png

(当我变大时,它似乎失去了边距并且还切割了TextBox的边缘)

【问题讨论】:

    标签: c# wpf dynamic textbox


    【解决方案1】:

    对于第一个问题,您可以动态生成文本框名称。

    int y = 0;
    
            private void btnAddTitle_Click(object sender, RoutedEventArgs e)
            {
                TextBox x = new TextBox();
                x.Name = "new_textbox" + y;
                x.TextWrapping = TextWrapping.Wrap;
                x.Height = 25;
                x.Width = 200;
                x.AcceptsReturn = true;
                x.Margin = new Thickness(10, 15, 950, 0);
                spStandard.Children.Add(x);
                y++;
            }
    

    【讨论】:

      猜你喜欢
      • 2013-07-08
      • 2014-01-09
      • 2020-11-28
      • 2012-08-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-20
      相关资源
      最近更新 更多