重置组框内成员的方法
本文关键字:成员 方法 | 更新日期: 2023-09-27 17:51:07
是否有方法清除groupBox内所有对象的属性?例如,清除所有文本框,取消选中所有复选框等,并将其设置为默认值。或者我应该一个一个地编码来清除它们?我想这样做的事件listview SelectedIndexChanged。
更新:ok,谢谢重播,我发现你可以在组框内选择控件非常简单。
foreach (Control ctrl in groupBox2.Controls)//this will only select controls of groupbox2
{
if (ctrl is TextBox)
{
(ctrl as TextBox).Text = "";
}
if (ctrl is CheckBox)
{
(ctrl as CheckBox).Checked = false;
}
if (ctrl is ComboBox)
{
(ctrl as ComboBox).SelectedIndex = -1;
}
//etc
}
最快的方法是:
Control myForm = Page.FindControl("Form1");
foreach (Control ctrl in myForm.Controls)
{
//Clears TextBox
if (ctrl is System.Web.UI.WebControls.TextBox)
{
(ctrl as TextBox).Text = "";
}
//Clears DropDown Selection
if (ctrl is System.Web.UI.WebControls.DropDownList)
{
(ctrl as DropDownList).ClearSelection();
}
//Clears ListBox Selection
if (ctrl is System.Web.UI.WebControls.ListBox)
{
(ctrl as ListBox).ClearSelection();
}
//Clears CheckBox Selection
if (ctrl is System.Web.UI.WebControls.CheckBox)
{
(ctrl as CheckBox).Checked = false;
}
//Clears RadioButton Selection
if (ctrl is System.Web.UI.WebControls.RadioButtonList)
{
(ctrl as RadioButtonList).ClearSelection();
}
//Clears CheckBox Selection
if (ctrl is System.Web.UI.WebControls.CheckBoxList)
{
(ctrl as CheckBoxList).ClearSelection();
}
}
您需要逐个清除组框内的所有控件
您必须创建这样的函数:
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 );
}
}
就像这样命名:
ClearControls(this);
这将递归地工作,所以如果你有任何面板,例如,有自己的一组控件要清除,这将清除那些。