Wednesday, April 2, 2008

Accessing a control on another form.

The way to do this is easy but hard to understand. I'll just tell ya the code, and if you need additional explanation let me know.

In Form1, have a function like:

public void ToggleButton(bool toggle)
{
Button1.Enabled = toggle
}


When you call Form2, do the following:
Form frmForm2 = new Form2();
frmForm2.Owner = this; //gives access to form 1's functions.
frmForm2.show();

Now in your close event of Form2, you can toggle the button by doing something like:
((Form1)this.Owner).ToggleButton(true);

Best of Luck to you,

Another way I found is below...

Declare Form1 in Program.cs file as below.

namespace TEST
{
static class Program
{

static public Form1 Frm;
//static public hudaldaa_list hudaldaa_list

///
/// The main entry point for the application.
///

[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Frm1= new Form1();
Application.Run(Frm1);
}
}
}

And button on From1 should be public.

Now you can change property of button on Form1 from anywhere like
Program.Frm1.button1.Enabled = false;

0 comments: