Accessing Objects Between Forms
Hi all,
I'm fairly new to C# and have run into a problem. I have one form mainForm which creates a new form optionsForm when a button is clicked.
private void button_Click(object sender, EventArgs e)
{
Options optionsForm = new Options(this);
DialogResult result = optionsForm.ShowDialog(this);
}
In the optionsForm, I've tried to pass mainForm by reference so I could access its objects.
private form_VisionSystem mainForm = null;
public Options(Form callingForm)
{
mainForm = callingForm as form_VisionSystem;
InitializeComponent();
}
I would like to access an object from mainForm and that object's methods from optionsForm. Is this possible, and if so how could I go about achieving this.
Any help would be greatly appreciated.
Regards,
Antonio.
Re: Accessing Objects Between Forms
Woops. I forgot to mention i used .NET 3.5.
Re: Accessing Objects Between Forms
you can create public properties to your forms, so that other forms can access it data or methods.
you can also use delegates for your methods.
Re: Accessing Objects Between Forms
Thanks for the reply,
I didn't know that public properties could be used to access methods,
I thought they were only for getting and setting variable values.
Regards,
Antonio.
Re: Accessing Objects Between Forms
Quote:
Originally Posted by
Antonio69
Thanks for the reply,
I didn't know that public properties could be used to access methods,
I thought they were only for getting and setting variable values.
Regards,
Antonio.
Properties are methods. I would not go this route as you will have to pass references to your main form back and forth and that gets messy fast. I would instead use events to tell your main form when to update the UI.