-
May 30th, 2010, 11:55 PM
#1
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.
-
May 31st, 2010, 12:02 AM
#2
Re: Accessing Objects Between Forms
Woops. I forgot to mention i used .NET 3.5.
-
May 31st, 2010, 01:32 AM
#3
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.
-
June 1st, 2010, 06:16 PM
#4
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.
-
June 1st, 2010, 06:48 PM
#5
Re: Accessing Objects Between Forms
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.
If you liked my post go ahead and give me an upvote so that my epee.... ahem, reputation will grow.
Yes; I have a blog too - http://the-angry-gorilla.com/
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|