在Windows窗体中多选择texbox集合并更改属性 [英] multi select texbox Collections in windowsform and change Attribute
本文介绍了在Windows窗体中多选择texbox集合并更改属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
你好
我有多个具有不同名称的文本框,我想更改属性.enable = true;
并使用foreach或
我该怎么办?
相同
hello
i have multi text box with different name i want to change attribute .enable=true;
and use foreach or for
how can i ?
same
foreach (Control tx in this.Controls)??????
{
Type c=tx.GetType();
if (c == typeof(TextBox))
????????????? i dont know
}
例如txt1.enable = true;
for example txt1.enable=true;
推荐答案
在这种情况下,我想花费更多的时间和精力来获得可以重新获得的东西.用过的.而且,我希望可以在任何特定种类的控件上进行操作的东西,并且可以在不对控件内的控件进行递归检查的情况下运行:
In this case, I''d want to invest a little more time and effort to get something that can be re-used. And, I''d want something that could operate on any particular kind of control, and that could be run with and without recursive checking for controls within controls:
private void SetControlsEnabledProperty(bool isEnabled, bool isRecursive, string typeToCheck, Control.ControlCollection theControls)
{
foreach (Control theControl in theControls)
{
// is there a better way to handle the
// the type checking required here ?
if (theControl.GetType().Name == typeToCheck)
{
theControl.Enabled = isEnabled;
}
else
{
if (isRecursive && theControl.HasChildren)
{
SetControlsEnabledProperty(isEnabled, isRecursive, typeToCheck, theControl.Controls);
}
}
}
}
}
我可能会像这样调用此函数以迭代窗体中的所有控件,并将每个TextBox的Enabled属性设置为true:
I might call this function like this to iterate all the Controls in a Form, and set every TextBox''s Enabled property to true:
SetControlsEnabledProperty(true, true, "TextBox", this.Controls);
foreach (Control control in this.Controls)
{
TextBox textBox = control as TextBox;
if (!object.ReferenceEquals(null, textBox))
textBox.Enabled = true;
}
这篇关于在Windows窗体中多选择texbox集合并更改属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文