开发者

Find control by name from Windows Forms controls

开发者 https://www.devze.com 2023-01-19 04:07 出处:网络
I have a list of my textbox names, and I 开发者_JAVA百科want to find a control by name. How is it possible?Use Control.ControlCollection.Find.

I have a list of my textbox names, and I 开发者_JAVA百科want to find a control by name. How is it possible?


Use 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. That gives you the control with name name.


TextBox tbx = this.Controls.Find("textBox1", true).FirstOrDefault() as TextBox;
tbx.Text = "found!";

If Controls.Find is not found "textBox1" => error. You must add code.

If(tbx != null)

Edit:

TextBox tbx = this.Controls.Find("textBox1", true).FirstOrDefault() as TextBox;
If(tbx != null)
   tbx.Text = "found!";
0

精彩评论

暂无评论...
验证码 换一张
取 消