2012-10-19 61 views
3

我有一个windows窗体应用程序,其中一些控件添加到设计器中。当我想改变的东西(像)从Form1.cs的内启用一个文本框从另一个类访问窗体控件​​

我简单地使用

textBox1.Enabled = true; 

,但现在我已经叫分离类的Class1.cs

怎么可能我启用textBox1从一个静态函数class1.cs?

{NOTE}我没有试过任何代码,因为我完全无法做到这一点。

+1

为ü想要什么原因去做?我想你应该改变你的设计 –

+0

改变我的设计!这是Windows窗体的本质,我无法访问Form1.cs之外的控件,因为它们是私人的。 –

+1

您是否尝试过将参考传递给班级中的控件?它可能是一个静态方法的论点。您需要提供class1.cs的上下文。它是派生的控件,你在哪里举办的这等 –

回答

8

编辑:很多的编辑。

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; 
    } 
} 
+0

实例,你需要有一个包含您要启用/禁用 –

+0

然后从Form类的实例应该创建文本框表单的参考,这是一个好主意? –

+0

它不工作:( –

1

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

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

让包含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 ; 
     } 
+0

我怎么能从一个静态函数class1.cs启用textBox1? –

+0

我会问为什么这是一个静态方法。如果你给我一个很好的理由,为什么然后我会考虑它 –

+0

好的理由是,我已经将所有的异步套接字构建为静态套接字,所以我不能再将其全部替换。另一个很好的理由是我想学习两种方法。 –

3

您可以将表格的实例传递到类

MyForm frm = new MyForm(); 

MyClass c = new MyClass(frm); 

然后你的类可以采取的实例和访问文本框

public class MyClass 
{ 

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

设计确实不好看

最好在窗体中调用该类,并基于val UE反馈,操纵文本框

//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; 

OR

int result = 0; 
Class1.SomeFunction(out result); 

if(result == 1) 
    textBox1.Enabled = true; 
else 
    textBox1.Enabled = false; 
+0

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

+0

您的静态函数可能会返回结果,或者您使用可以从静态函数中操作的out变量 - 这是关于设计 – codingbiz

2

你真的不应该改变您的Form中的UI控件从您的类1,而是创建一个方法或属性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; } } 
} 

然后在您的形式:

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); 
-1

这是你应该怎么做:

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;public static Form1 form = null;

好运...写的哈桑Eskandari :)

0

这只是另一种方法:

TextBox t = Application.OpenForms["Form1"].Controls["textBox1"] as TextBox; 
相关问题