c# Windows窗体-如何在运行时创建新的文本框/侧标签(也在运行时创建)
本文关键字:运行时 创建 标签 文本 窗体 Windows | 更新日期: 2023-09-27 18:12:05
这是可能的,例如:
form start with 1 textbox(name: textbox1) and 1 label(name: label1)在运行时创建文本框和标签(side-side)所以在运行时我们可以输入
label1 - textbox1
label2 - textbox2
label3 - textbox3
label4 - textbox4
在编译可执行文件之前,如何在代码中引用这些未来文本框/标签而不会出现这些文本框/标签不存在的错误?
大家都知道,我是这样在运行时创建新的文本框和标签的
n++;
TextBox txt = new TextBox();
txt.Name = "textbox" + n;
txt.Text = "";
txt.Size = new System.Drawing.Size(189, 26);
txt.Location = new Point(87, n2);
testelogico = txt.Name;
gpbCategoria.Controls.Add(txt);
txt.TextChanged += new EventHandler(new_onchange);
txt.Leave += new EventHandler(erase_onLeave);
Label lbl = new Label();
lbl.Name = "label" + n;
lbl.Text = "Acessório Nº" + n + ":";
lbl.Location = new Point(4, n2 + 5);
gpbCategoria.Controls.Add(lbl);
在代码的另一部分,我想引用例如:
If (textbox4.Text == "" && label4.Name == "Acessório Nº4:")
{
gpbCategoria.Controls.Remove(textbox4);
gpbCategoria.Controls.Remove(label4);
}
但是我会有错误,因为这些标签还不存在(只会在运行时创建)
您正在动态生成控件,因此编译器在创建textBox4
之前不知道它是什么。你所能做的就是在运行时根据控件的名称搜索该控件:
TextBox textbox4 = (TextBox)this.Controls.Find("textbox4", false).FirstOrDefault();
if (textbox4 == null)
{
throw new Exception("Could not find textbox4.");
}
这将在Form.Controls
中搜索textbox4
,如果不存在则抛出异常。您可以对labels
或任何其他形式的控件遵循相同的模式。
您可以通过名称找到文本框:
var textbox = this.Controls.OfType<TextBox>().Single(ctr => ctr.Name == "textboxname");
在UserControl中放置Label - TextBox比较好。
让UserControl拥有通过构造函数传递的索引号
public class MyUserControl : UserControl
{
private readonly int index;
public MyUserControl(int index)
{
this.index = index;
InitializeComponent(); // will init you sub controls: label and textbox
// set name to label
}
public int Index
{
get { return index; }
}
}
使用已经建议的方法通过控件集合中的索引查找您的用户控件,如果找到则删除它。
嗨@programmer93和@Jonesy,感谢帮助,现在它工作得很好,看到我的最终代码(可以帮助有同样疑问的人,我有)
TextBox txtAcessorio4 = (TextBox)gpbCategoria.Controls.Find("txtAcessorio4", false).FirstOrDefault();
Label lblAcessorio4 = (Label)gpbCategoria.Controls.Find("lblAcessorio4", false).FirstOrDefault();
if (txtAcessorio4 != null && txtAcessorio4.Text == "" && lblAcessorio4.Name == "lblAcessorio4")
{
MessageBox.Show("Perfect");
}
如果你使用的是。net framework 4.0或更高版本,那么你可以使用动态关键字:下面是完整的代码供您参考:
public class Class1
{
// Declare all the controls as dynamic
dynamic textbox1, textbox2, textbox3, textbox4;
dynamic label1, label2, label3, label4;
public Class1()
{
// Create the actual object type, which they will hold at Run time.
textbox1 = textbox2 = textbox3 = textbox4 = new TextBox();
label1 = label2 = label3 = label4 = new Label();
// Loop through to create controls at Runtime.
n++;
TextBox txt = new TextBox();
txt.Name = "textbox" + n;
txt.Text = "";
txt.Size = new System.Drawing.Size(189, 26);
txt.Location = new Point(87, n2);
testelogico = txt.Name;
gpbCategoria.Controls.Add(txt);
txt.TextChanged += new EventHandler(new_onchange);
txt.Leave += new EventHandler(erase_onLeave);
Label lbl = new Label();
lbl.Name = "label" + n;
lbl.Text = "Acessório Nº" + n + ":";
lbl.Location = new Point(4, n2 + 5);
gpbCategoria.Controls.Add(lbl);
}
public void Foo()
{
//Throw exception if controls are not initialized yet.
if (textbox4 == null || label4 == null)
{
throw new Exception("Controls not initialized.");
}
else
{
// You can access the control propoties similar to normal controls.
if (textbox4.Text == "" && label4.Name == "Acessório Nº4:")
{
gpbCategoria.Controls.Remove(textbox4);
gpbCategoria.Controls.Remove(label4);
}
}
}
}