多个文本框中的只读属性[重复]

问题描述 投票:0回答:3

这个问题在这里已有答案:

我正在制作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识别为有效参数。

c# winforms
3个回答
0
投票

打电话使用这个。

  DoReadOnly(this)

如果方法是在Form类上


0
投票

只传递'this',该对象是您当前的形式

public void getData(){
   DoReadOnly(this);
}

0
投票

使用DoReadOnly(this);而不是DoReadOnly(this.Form);

另外,为什么getDate如果要放置或更改属性,请使用setData

© www.soinside.com 2019 - 2024. All rights reserved.