Foreach Control in form, how can I do something to all the TextBoxes in my Form?

后端 未结 14 1520
野的像风
野的像风 2020-11-28 08:13

How can I use a Foreach Statement to do something to my TextBoxes?

foreach (Control X in this.Controls)
{
    Check if the controls is a TextBox, if it is de         


        
14条回答
  •  孤城傲影
    2020-11-28 08:37

    private IEnumerable GetTextBoxes(Control control)
    {
        if (control is TextBox textBox)
        {
            yield return textBox;
        }
    
        if (control.HasChildren)
        {
            foreach (Control ctr in control.Controls)
            {
                foreach (var textbox in GetTextBoxes(ctr))
                {
                    yield return textbox;
                }
            }
        }
    }
    

提交回复
热议问题