[英]Find control by name from Windows Forms controls
I have a list of my textbox names, and I want to find a control by name.我有一个文本框名称列表,我想按名称查找控件。 How is it possible?
这怎么可能?
Use Control.ControlCollection.Find .使用Control.ControlCollection.Find 。
TextBox tbx = this.Controls.Find("textBox1", true).FirstOrDefault() as TextBox;
tbx.Text = "found!";
EDIT for asker:编辑提问者:
Control[] tbxs = this.Controls.Find(txtbox_and_message[0,0], true);
if (tbxs != null && tbxs.Length > 0)
{
tbxs[0].Text = "Found!";
}
You can use:您可以使用:
f.Controls[name];
Where f
is your form variable.其中
f
是您的表单变量。 That gives you the control with name name
.这为您提供了名称
name
的控件。
TextBox tbx = this.Controls.Find("textBox1", true).FirstOrDefault() as TextBox;
tbx.Text = "found!";
If Controls.Find is not found "textBox1" => error.如果 Controls.Find 未找到“textBox1”=> 错误。 You must add code.
您必须添加代码。
If(tbx != null)
Edit:编辑:
TextBox tbx = this.Controls.Find("textBox1", true).FirstOrDefault() as TextBox;
If(tbx != null)
tbx.Text = "found!";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.