这个问题在这里已有答案:
我正在制作Windows表单应用程序。
好吧,C#编程对我来说是新的,也许这是一个愚蠢的问题,但我如何在多个文本框元素中应用ReadOnly
属性?我试过这段代码:
public void DoReadOnly(Control control){
foreach (Control c in control.Controls){
if (c.Controls != null && c.Controls.Count > 0){
DoReadOnly(c);
}
else if (c is TextBox){
(c as TextBox).ReadOnly = true;
}
}
}
public void getData(){
DoReadOnly(this.Form);
}
麻烦的是,当我调用doReadOnly函数时,我不知道应该放哪个参数。 Visual Studio不会将this.Form
识别为有效参数。
打电话使用这个。
DoReadOnly(this)
如果方法是在Form类上
只传递'this',该对象是您当前的形式
public void getData(){
DoReadOnly(this);
}
使用DoReadOnly(this);而不是DoReadOnly(this.Form);
另外,为什么getDate如果要放置或更改属性,请使用setData