How to clear the text of all textBoxes in the form?

后端 未结 9 1530
长发绾君心
长发绾君心 2020-11-27 05:15
private void CleanForm()
{
    foreach (var c in this.Controls)
    {
        if (c is TextBox)
        {
            ((TextBox)c).Text = String.Empty;
        }
            


        
9条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-27 06:06

    And this for clearing all controls in form like textbox, checkbox, radioButton

    you can add different types you want..

    private void ClearTextBoxes(Control control)
        {
            foreach (Control c in control.Controls)
            {
                if (c is TextBox)
                {
                    ((TextBox)c).Clear();
                }
    
                if (c.HasChildren)
                {
                    ClearTextBoxes(c);
                }
    
    
                if (c is CheckBox)
                {
    
                    ((CheckBox)c).Checked = false;
                }
    
                if (c is RadioButton)
                {
                    ((RadioButton)c).Checked = false;
                }
            }
        }
    

提交回复
热议问题