Winforms C# - 透過 panel 清除表單內容

Panel 通常會使用 Control 來控制內部元件,例如:

MainPanel.Controls.Find("TGNAME", true)[0].Text

這裡說明如何透過 panel 來清除表單群組,

製作一個清除欄位內容(TextBox, CheckBox, RichTextBox, ComboBox, MaskedTextBox) 的方法

private void btnClear_Click(object sender, EventArgs e)
{

    foreach (Control Ctrl in this.mainSearhpanel.Controls)
    {
        switch (Ctrl.GetType().ToString())

        {
            case "System.Windows.Forms.CheckBox":
                ((CheckBox)Ctrl).Checked = false;
                break;

            case "System.Windows.Forms.TextBox":
                ((TextBox)Ctrl).Text = "";
                break;

            case "System.Windows.Forms.RichTextBox":
                ((RichTextBox)Ctrl).Text = "";
                break;

            case "System.Windows.Forms.ComboBox":
                ((ComboBox)Ctrl).SelectedIndex = -1;
                ((ComboBox)Ctrl).SelectedIndex = -1;
                break;

            case "System.Windows.Forms.MaskedTextBox":

                ((MaskedTextBox)Ctrl).Text = "";
                break;

        }

    }
}