Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to access public (non static) function in non instantiated form

Status
Not open for further replies.

rickinger

Programmer
Feb 11, 2004
20
NL
Hi fellows,

my MDI application opens it's main container form with the regular command "Application.Run(new MainWindow());" (extendet form class) and I would like to access some public function of that form from somewhere else.
Problem: I can not get to the MainWindow object though; there seems to be a reference MainWindow.ActiveForm but that is only a form object and non the extendet MainWindow object so I can not find my own public function there. Also this function can not be static since I am modifying some (this.)menu entries with it...
What am I missing here? How do I get the MainWindow object reference?
 
Take this code fragment:
[tab]new MainWindow()
and assign it to a variable of the type of your form.
After calling Application.Run(), you can pass parameters to it like any other form.
Code:
MainWindow mmf = new MainWindow();
Application.Run(mmf);
mmf.SetAPropertyHere = 42;
Chip H.


____________________________________________________________________
Click here to learn Ways to help with Tsunami Relief
If you want to get the best response to a question, please read FAQ222-2244 first
 
Hi chiph,

thanks, I got a handle now, but I can make it public so I can access the form from anywhere else? It seems like I can not declare mmf public in the form class (I guess it's a collision between the form instance and the main function being static)?

Regards
rickinger
 
What you want then is something called a Singleton (do a google). It's a design pattern that allows you to do this. What you'd do is wrap your MainWindow variable (mmf) inside the singleton class. Afterwards whenever anyone needed to access it, they'd go thru the singleton.

Chip H.


____________________________________________________________________
Click here to learn Ways to help with Tsunami Relief
If you want to get the best response to a question, please read FAQ222-2244 first
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top