Find control by name from Windows Forms controls

前端 未结 3 906
甜味超标
甜味超标 2020-11-22 08:03

I have a list of my textbox names, and I want to find a control by name. How is it possible?

3条回答
  •  不要未来只要你来
    2020-11-22 08:16

    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!";
    }
    

提交回复
热议问题