以编程方式禁用所有文本框、日期时间选择器、组合框
Disable all texboxes, datetimepicker, comboboxes programatically
有没有一种方法可以禁用表单加载中的所有文本框、日期时间选择器、组合框?
我的 form_load 有这样的情况:
private void frmTWLeasing_Load(object sender, EventArgs e)
{
//this.Text = sTitle;
this.txtNinja1.Text = sEnable;
if (sEnable == "Disabled")
{
disable all textboxes, datetimepicker, comboboxes or gridviews
}
else
{
enable all textboxes, datetimepicker, comboboxes or gridviews
}
}
我有如此多的文本框、日期时间选择器和组合框,包括数据网格视图,那么是否有任何语法可以调用所有文本框、日期时间选择器、组合框,以便我可以一次禁用它?很难一一禁用它..希望你们能帮助我提前谢谢!!
您可以使用 OfType(),然后在 Where()
中获取特定的类型控制,然后迭代并禁用它们或根据需要启用它们,如下所示:
if (sEnable == "Disabled")
{
var controls = this.Controls.OfType<Control>().Where(x=>x is TextBox || x is ComboBox || x is DateTimePicker);
foreach(var control in controls)
control.Enabled = false;
}
您需要在 class
中添加 using System.Linq
如果您在嵌套控件中有控件,意味着在某些用户控件或其他控件中,那么您需要像 this SO post (How to get ALL child controls of a Windows Forms form of a specific type)
那样递归检查
有没有一种方法可以禁用表单加载中的所有文本框、日期时间选择器、组合框?
我的 form_load 有这样的情况:
private void frmTWLeasing_Load(object sender, EventArgs e)
{
//this.Text = sTitle;
this.txtNinja1.Text = sEnable;
if (sEnable == "Disabled")
{
disable all textboxes, datetimepicker, comboboxes or gridviews
}
else
{
enable all textboxes, datetimepicker, comboboxes or gridviews
}
}
我有如此多的文本框、日期时间选择器和组合框,包括数据网格视图,那么是否有任何语法可以调用所有文本框、日期时间选择器、组合框,以便我可以一次禁用它?很难一一禁用它..希望你们能帮助我提前谢谢!!
您可以使用 OfType(),然后在 Where()
中获取特定的类型控制,然后迭代并禁用它们或根据需要启用它们,如下所示:
if (sEnable == "Disabled")
{
var controls = this.Controls.OfType<Control>().Where(x=>x is TextBox || x is ComboBox || x is DateTimePicker);
foreach(var control in controls)
control.Enabled = false;
}
您需要在 class
中添加using System.Linq
如果您在嵌套控件中有控件,意味着在某些用户控件或其他控件中,那么您需要像 this SO post (How to get ALL child controls of a Windows Forms form of a specific type)
那样递归检查