我目前正在处理文本框上的可见属性。下面我复制粘贴了我的代码片段。我的表单中有几个文本框。尝试编写它会变得非常乏味,因为我在下面为所有文本框编写了它。有没有办法将我的代码压缩到几行以使文本框可见?
public void makeVisible()
{
textBox1.Visible = true;
textBox2.Visible = true;
textBox3.Visible = true;
textBox4.Visible = true;
//etc.
}
最佳答案
试试这个:
foreach(Control c in Controls)
{
TextBox tb = c as TextBox;
if (tb !=null) tb.Visible = false; //or true, whatever.
}
对于有限的文本框:
int count = 0;
int txtBoxVisible = 4;
foreach(Control c in Controls)
{
if(count <= txtBoxVisible)
{
TextBox tb = c as TextBox;
if (tb !=null) tb.Visible = false; //or true, whatever.
count++;
}
}
您可以根据需要设置txtBoxVisible
。
关于c# - 文本框可见属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14918757/