Winform清空容器中所有控件中的值

在Winfrom开发中,在用户提交数据后,有时需要清空界面的值
可以使用下面的函数进行清空

 /// 
            /// 清除容器里面某些控件的值
            /// 
            /// 容器名称
    public void ClearControlValue(Control parContainer)
    {
        for (int index = 0; index < parContainer.Controls.Count; index++)
        {
            // 如果是容器内部控件,则递归调用自己
            if (parContainer.Controls[index].HasChildren)
            {
                ClearControlValue(parContainer.Controls[index]);
            }
            else
            {
                switch (parContainer.Controls[index].GetType().Name)
                {
                    case "TextBox":
                        (parContainer.Controls[index] as TextBox).Text = "";
                        break;


                case "RadioButton":
                    (parContainer.Controls[index] as RadioButton).Checked = false;
                    break;


                case "CheckBox":
                    (parContainer.Controls[index] as CheckBox).Checked = false;
                    break;


                case "ComboBox":
                    (parContainer.Controls[index] as ComboBox).Text = "";
                    (parContainer.Controls[index] as ComboBox).SelectedIndex = -1;
                    break;


                case "DataGridView":
                    (parContainer.Controls[index] as DataGridView).DataSource = null;
                    break;

                case "ListBox":
                    (parContainer.Controls[index] as ListBox).Items.Clear();
                    break;
                case "DateTimePicker":
                    (parContainer.Controls[index] as DateTimePicker).Checked = false;
                    break;
            }
        }
    }
}

非常的简单好用.

//原文:https://blog.csdn.net/yanguan55/article/details/8663392

你可能感兴趣的:(C#笔记)