它的可能性例如:
表单以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);
}
但我会有错误,因为这些标签尚不存在(只会在运行时创建)
答案 0 :(得分:2)
您正在动态生成控件,因此编译器在创建它之前不知道textBox4
是什么。您可以做的是在运行时通过其名称搜索该控件:
TextBox textbox4 = (TextBox)this.Controls.Find("textbox4", false).FirstOrDefault();
if (textbox4 == null)
{
throw new Exception("Could not find textbox4.");
}
这将在textbox4
中搜索Form.Controls
,如果不存在则会抛出异常。您可以对labels
或表单中的任何其他控件使用相同的模式。
答案 1 :(得分:0)
您可以按名称找到文本框:
var textbox = this.Controls.OfType<TextBox>().Single(ctr => ctr.Name == "textboxname");
答案 2 :(得分:0)
如果您使用的是.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);
}
}
}
}
答案 3 :(得分:0)
将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集合中的索引查找用户控件,如果找到则将其删除。
答案 4 :(得分:0)
嗨@ 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");
}