View previous topic :: View next topic |
Author |
Message |
Deltron Z Expert Cheater
Reputation: 1
Joined: 14 Jun 2009 Posts: 164
|
Posted: Tue Dec 14, 2010 5:31 am Post subject: [C#] Best way to communicate between forms? |
|
|
I have one main form (and possibly more in the future) from which other sub-forms open (and they might have sub-forms of themselves) and I wonder what is, in your opinion, the best way to communicate between them?
I thought of passing the main form as a parameter to the constructors of the sub-forms, but it doesn't seem like a good way, especially if I'm going to need to communicate between other, distinct, sub-forms, not to mention I have to double check the input, or make a few methods, but it seems more like functional programming than object oriented programming...
Perhaps I can..:
-Create a static class (or Properties.Settings) for global settings. Cons: every change of data is needed to be copied to the class, I'm looking for something a bit more comfrotable and elegant.
-Use the ugly way of accessing the controls from Application.OpenForms - fixes the problem of passing the main form as parameter. Cons: not very stable.
-Do something else I haven't thought of. suggestions? Cons: don't know what it is yet.
|
|
Back to top |
|
 |
Jorg hi I post too much
Reputation: 7
Joined: 24 Dec 2007 Posts: 2276 Location: Minnesota
|
Posted: Tue Dec 14, 2010 6:56 am Post subject: |
|
|
When the main form is going to show the 2nd form, pass the second form to a variable of the first form.
Code: | Form2 variable = new Form2();
variable.show(); |
If you want to allow dual connection, add a variable to the 2nd form to store the 1st form.
Code: | Form2 variable = new Form2();
variable.mainForm=this;
variable.show(); |
Make sure the access objects of each form are not private, unless intended.
_________________
CEF will always stay alive. |
|
Back to top |
|
 |
Deltron Z Expert Cheater
Reputation: 1
Joined: 14 Jun 2009 Posts: 164
|
Posted: Tue Dec 14, 2010 7:44 am Post subject: |
|
|
I know all that, I even said I thought about that... but what if I want to communicate with two or more forms, which not nesecceraly have the same parent? I want some kind of global settings, I'm asking to consider my options before choosing what's best, I already have quite a few options, I just thought it would be a good idea to consult first.
|
|
Back to top |
|
 |
Jorg hi I post too much
Reputation: 7
Joined: 24 Dec 2007 Posts: 2276 Location: Minnesota
|
Posted: Tue Dec 14, 2010 5:14 pm Post subject: |
|
|
No global variables in C#, because its OO. Just make a static class to store an array of all the forms.
You basically already have an answer for your question. Whats the point of 'comfortable & elegant' when there are equal results.
_________________
CEF will always stay alive. |
|
Back to top |
|
 |
atom0s Moderator
Reputation: 205
Joined: 25 Jan 2006 Posts: 8587 Location: 127.0.0.1
|
Posted: Wed Dec 15, 2010 12:29 am Post subject: |
|
|
Depending on how in depth your sharing needs to be, you may want to look into singletons. Also since you're wanting it to be between forms, you need to ensure thread-safety as well.
http://msdn.microsoft.com/en-us/library/ff650316.aspx
_________________
- Retired. |
|
Back to top |
|
 |
Deltron Z Expert Cheater
Reputation: 1
Joined: 14 Jun 2009 Posts: 164
|
Posted: Wed Dec 15, 2010 12:09 pm Post subject: |
|
|
Well, I don't plan to open many instances of the same form, but I guess this isn't the only use, but thread-saftey calls for methods who access variables from different threads and such...
Thanks, I'll use this method with Properties.Settings, I guess.
I assume there isn't a very nice way to acheive fast and easy communication between forms, so I'll just do the other things I planned to.
|
|
Back to top |
|
 |
|