我编写了一个动态文本框按钮。当我单击按钮时,将在我的程序上创建一个动态文本框。我还有两个文本框,它们在程序中是静态的。
现在的问题是: 当我在两个静态文本框中添加值时,可以使用MessageBox.Show向我显示这些值。单击“添加动态文本框”按钮后,该值为NULL。因此,静态文本框中的值为空。动态文本框不是NULL,我可以向我展示它们的值。
{
公共局部类Form1:表单 静态整数i = 1;
public Form1()
{
InitializeComponent();
}
private void maskedTextBox1_MaskInputRejected(object sender, MaskInputRejectedEventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
if (i < 8)
{
InitializeComponent();
TextBox tb = new TextBox();
Label lb = new Label { };
lb.Text = i + ". label";
lb.Name = "label" + i;
tb.Name = "textbox" + i ;
tb.Width = 238;
tb.Height = 35;
lb.Width = 238;
lb.Height = 35;
tb.Font = new Font(tb.Font.FontFamily, 18);
lb.Font = new Font(lb.Font.FontFamily, 18);
Point p = new Point(223, 144 + (40 * i));
Point p2 = new Point(50, 144 + (40 * i));
lb.Location = p2;
tb.Location = p;
this.Controls.Add(tb);
this.Controls.Add(lb);
i++;
}
else
{
MessageBox.Show("Maximum reached");
}
}
public void insert()
{
TextBox tb1 = this.Controls.Find("textbox1", false).FirstOrDefault() as TextBox;
TextBox tb2 = this.Controls.Find("textbox2", false).FirstOrDefault() as TextBox;
TextBox tb3 = this.Controls.Find("textbox3", false).FirstOrDefault() as TextBox;
TextBox tb4 = this.Controls.Find("textbox4", false).FirstOrDefault() as TextBox;
TextBox tb5 = this.Controls.Find("textbox5", false).FirstOrDefault() as TextBox;
TextBox tb6 = this.Controls.Find("textbox6", false).FirstOrDefault() as TextBox;
TextBox tb7 = this.Controls.Find("textbox7", false).FirstOrDefault() as TextBox;
MessageBox.Show(textboxstatic1.ToString());
}
预期结果:输入值 添加动态文本框后的结果:无值