Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing Form's Controls from another class

Tags:

c#

I have a windows forms application with some controls added to the designer. When I want to change something (LIKE) enabling 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 try any code because I am totally clueless about doing this.

like image 762
Roman Ratskey Avatar asked Oct 19 '12 22:10

Roman Ratskey


People also ask

How to access the controls of a form?

The Form is a container type. You cannot directly access the controls on the form. You have to use f.Controls () method. Often developers need to access a specific control from outside, maybe from another class.

How to access form components directly from a class?

If you actually wanted to access the form components directly from a class (which you don't) you would use the variable that instantiates your form. Depending on which way you want to go you'd be better of either sending the text of a control or whatever to a method in your classes eg

How do I access the controls of a windowsformapliccation?

In VB.NET when I add a class to an application type WindowsFormApliccation I can access the controls directly, using the form name followed by the name of the control I want. For example: I have a textbox on the form and add a class to the project, at the class I have the method below.

How to access a form in C #?

If you want to access a form in c# you may have to do. The Form1 is the form user created in design view. then access the methods through instance "f". The Form is a container type. You cannot directly access the controls on the form.


3 Answers

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()
    {
        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;
    }
}
like image 53
LightStriker Avatar answered Oct 17 '22 23:10

LightStriker


This is just another method:

TextBox t = Application.OpenForms["Form1"].Controls["textBox1"] as TextBox;
like image 31
vr_driver Avatar answered Oct 18 '22 01:10

vr_driver


You can pass the instance of your Form to the class

 MyForm frm = new MyForm();

 MyClass c = new MyClass(frm);

Then your class can take that instance and access the textbox

 public class MyClass
 {

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

The design does not look OK

It is better to call the class in your form and based on the value returned, manipulate the textbox

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

Usage

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;
like image 4
codingbiz Avatar answered Oct 18 '22 00:10

codingbiz