[英]C# Windows Form - How reference created in runtime new textbox/side label (also created in runtime)?
它的可能性例如:
表单以1个文本框(名称:textbox1)开头,1个标签(名称:label1)在运行时创建文本框和标签(侧面)所以在运行时我们可以有
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.");
}
这将搜索textbox4
在Form.Controls
,将抛出一个异常,如果它不存在。 您可以对表单中的labels
或任何其他控件使用相同的模式。
你可以按名称找到文本框:
var textbox = this.Controls.OfType<TextBox>().Single(ctr => ctr.Name == "textboxname");
如果您使用的是.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);
}
}
}
}
将Label-TextBox削减到UserControl会好得多。
让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; }
}
}
使用已建议的方法通过Controls集合中的索引查找用户控件,如果找到则将其删除。
嗨@ 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");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.