Roman Ratskey Roman Ratskey - 2 months ago 16x
C# Question

Accessing Form's Controls from another class

I have a windows forms application with some controls added to the designer. When i want to change something (LIKE) Enable a text box from inside the Form1.cs

i simply use

textBox1.Enabled = true;

but now i have a separated class called class1.cs

How could i enable textBox1 from a static function class1.cs ?

{NOTE} I did not tried any code because i am totally clueless about doing this.


EDIT: Lot of edit.

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()
        form = this;

    // Static method, call the non-static version if the form exist.
    public static void EnableStaticTextBox(bool enable)
        if (form != null)

    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;