Method to reset members inside groupBox

前端 未结 3 1684
醉酒成梦
醉酒成梦 2020-12-22 05:35

is there any method for groupBox to clear all properties of objects inside groupBox. for example clear all textboxes, deselect all checkboxes etc. and set them to default. o

3条回答
  •  渐次进展
    2020-12-22 06:10

    You would have to make a function like this:

    private void ClearControls(Control control)
    {
        var textbox = control as TextBox;
        if (textbox != null)
            textbox.Text = string.Empty;
    
        var dropDownList = control as DropDownList;
        if (dropDownList != null)
            dropDownList.SelectedIndex = 0;
    
        // And add any other controls
        // ...
    
        foreach( Control childControl in control.Controls )
        {
            ClearControl( childControl );
        }
    }
    

    Just call it like this:

    ClearControls(this);
    

    This will work recursively, so if you have any panels, for example, with their own set of controls to clear, this will clear those too.

提交回复
热议问题