Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


[C#] Best way to communicate between forms?

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
Deltron Z
Expert Cheater
Reputation: 1

Joined: 14 Jun 2009
Posts: 164

PostPosted: Tue Dec 14, 2010 5:31 am    Post subject: [C#] Best way to communicate between forms? Reply with quote

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. Razz suggestions? Cons: don't know what it is yet. Sad
Back to top
View user's profile Send private message
Jorg hi
I post too much
Reputation: 7

Joined: 24 Dec 2007
Posts: 2276
Location: Minnesota

PostPosted: Tue Dec 14, 2010 6:56 am    Post subject: Reply with quote

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
View user's profile Send private message
Deltron Z
Expert Cheater
Reputation: 1

Joined: 14 Jun 2009
Posts: 164

PostPosted: Tue Dec 14, 2010 7:44 am    Post subject: Reply with quote

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
View user's profile Send private message
Jorg hi
I post too much
Reputation: 7

Joined: 24 Dec 2007
Posts: 2276
Location: Minnesota

PostPosted: Tue Dec 14, 2010 5:14 pm    Post subject: Reply with quote

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
View user's profile Send private message
atom0s
Moderator
Reputation: 205

Joined: 25 Jan 2006
Posts: 8587
Location: 127.0.0.1

PostPosted: Wed Dec 15, 2010 12:29 am    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
Deltron Z
Expert Cheater
Reputation: 1

Joined: 14 Jun 2009
Posts: 164

PostPosted: Wed Dec 15, 2010 12:09 pm    Post subject: Reply with quote

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
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites