如何为全局赢表中的所有文本框设置背景色?我们可以像在全局变量中那样设置它并在需要时使用吗?我需要从全局变量中在form_load中设置控件的背景色,而不是编写
mytextbox1.BackColor = Color.Red;
mytextbox2.BackColor = Color.Red;
private void SetRedColorToTextBoxes()
{
Action<Control.ControlCollection> func = null;
func = (controls) =>
{
foreach (Control control in controls)
if (control is TextBox)
(control as TextBox).BackColor = Color.Red;
else
func(control.Controls);
};
func(Controls);
}
并SetRedColorToTextBoxes()
在表单加载中调用函数。
private void YourForm_Load(object sender, EventArgs e)
{
SetRedColorToTextBoxes();
}
编辑添加一个.cs文件,并将代码放在此处。
class Helper
{
public void SetRedColorToTextBoxes(Form frm)
{
Action<Control.ControlCollection> func = null;
func = (controls) =>
{
foreach (Control control in controls)
if (control is TextBox)
(control as TextBox).BackColor = Color.Red;
else
func(control.Controls);
};
func(frm.Controls);
}
}
并将其称为表单加载:
private void YourForm_Load(object sender, EventArgs e)
{
// this means instance of currentform.
(new Helper()).SetRedColorToTextBoxes(this);
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句