从另一个类访问表单控件

我有一个Windows窗体应用程序,其中一些控件添加到设计器中。 当我想要更改某些内容(LIKE)时从Form1.cs中启用一个文本框

我只是用

textBox1.Enabled = true; 

但现在我有一个名为class1.cs的独立类

我如何从静态函数class1.cs启用textBox1?

{注意}我没有尝试任何代码,因为我完全不知道这样做。

编辑:很多编辑。

 public partial class Form1 : Form { // Static form. Null if no form created yet. private static Form1 form = null; private delegate void EnableDelegate(bool enable); public Form1() { InitializeComponent(); form = this; } // Static method, call the non-static version if the form exist. public static void EnableStaticTextBox(bool enable) { if (form != null) form.EnableTextBox(enable); } private void EnableTextBox(bool enable) { // If this returns true, it means it was called from an external thread. if (InvokeRequired) { // Create a delegate of this method and let the form run it. this.Invoke(new EnableDelegate(EnableTextBox), new object[] { enable }); return; // Important } // Set textBox textBox1.Enabled = enable; } } 

您可以将表单的实例传递给类

  MyForm frm = new MyForm(); MyClass c = new MyClass(frm); 

然后,您的类可以获取该实例并访问文本框

  public class MyClass { public MyClass(MyForm f) { f.TextBox1.Enabled = false; } } 

设计看起来不太好

最好在表单中调用类并根据返回的值操作文本框

 //MyForm Class MyClass c = new MyClass(); c.DoSomethings(); if(c.getResult() == requiredValue) textBox1.enabled = true; else textBox1.enabled = false; //MyForm Class ends here 

UPDATE

 public class Class1 { public static int SomeFunction() { int result = 1; return result; } public static void SomeFunction(out int result) { result = 1; } } 

用法

 if(Class1.SomeFunction() == 1) textBox1.Enabled = true; else textBox1.Enabled = false; 

要么

 int result = 0; Class1.SomeFunction(out result); if(result == 1) textBox1.Enabled = true; else textBox1.Enabled = false; 

你可以让你的class1有一个事件来启用文本框。

 public class Class1 { public event Action subscribe ; private void raiseEvent() { var handler = subscribe ; if(handler!=null) { handler(this,EventArgs.Empty);//Raise the enable event. } } } 

让包含TextBox的类以某种方式订阅它。 在TextBox包装器类中

  public class TextBoxWrapper public void EnablePropertyNotification(object sender, EventArgs args) { TextBox1.Enabled = true ; //Enables textbox when event is raised. } public TextBoxWrapper() { class1Instance.subscribe+=EnablePropertyNotification ; } 

您不应该从class1更改Form UI控件,而是在class1中创建一个方法或属性,以告知是否应该启用文本框。

例:

 // I changed the name class1 to MySettings public class MySettings { public bool ShouldTextBoxBeEnabled() { // Do some logic here. return true; } // More generic public static bool SetTextBoxState(TextBox textBox) { // Do some logic here. textBox.Enabled = true; } // Or static property (method if you like) public static StaticShouldTextBoxBeEnabled { get { return true; } } } 

然后在你的forms:

 MySettings settings = new MySettings(); textBox1.Enabled = settings.ShouldTextBoxBeEnabled(); // Or static way textBox1.Enabled = MySettings.StaticShouldTextBoxBeEnabled; // Or this way you can send in all textboxes you want to do the logic on. MySettings.SetTextBoxState(textBox1); 

这只是另一种方法:

 TextBox t = Application.OpenForms["Form1"].Controls["textBox1"] as TextBox; 

我不得不在工作中这样做,并没有发现任何这些答案与我最终做的相符,所以我展示了我是如何使它工作的。

首先,在load事件中初始化类的副本。

 NameOfClass newNameofClass; 

然后你想绑定到你的类(在load事件中):

 textBox1.DataBindings.Add(new Binding("Enabled", newNameofClass, "textBox1Enabled")); 

在您的课程中,请执行以下操作:

 private bool textBox1Enabled = false; public bool TextBox1Enabled { get { return textBox1Enabled; } set { textBox1Enabled = value; } } 
  • false设置会将文本框初始化为禁用
  • 如果要在默认情况下启用,请将textBox1Enabled设置为true。
  • 如果您有其他逻辑来启用/禁用文本框,只需相应地修改textBox1Enabled的值。

这是你应该怎么做:我在我的表单类中编写了下面的代码:

 public static Form1 form = null; private delegate void SetImageDelegate(Image image); public Form1() { InitializeComponent(); form = this; } public static void SetStaticImage(Image image) { if (form != null) form.pic1.Image = image; } private void setImage(Image img) { // If this returns true, it means it was called from an external thread. if (InvokeRequired) { // Create a delegate of this method and let the form run it. this.Invoke(new SetImageDelegate(setImage), new object[] { img }); return; // Important } // Set textBox pic1.Image = img; } 

并且下面的代码应该在另一个类中:

 Form1 frm= Form1.form; frm.pic1.Image = image; 

请注意,我更改了private static Form1 form = null; to public static Form1 form = null;

祝你好运……由Hassan Eskandari撰写:)