Understanding application level variables and functions compared to C++
-
i am a newbie at c#, but have a good amount of time with vc++ development.. can someone help me with some questions? in vc++ my application could have variables and functions like:
CCompanyStore* myCompanyStore;
void ShowAboutDialog();and i would call them throughout the program like:
CMyApp* pApp = (CMyApp*)AfxGetApp();
pApp->ShowAboutDialog();i would use these types of functions for replacing views in my mainframe and such as well.. but i dont understand how to call a function from my base form without doing something stupid like calling
this.Parent.Parent.SetStatusText("hello");
.. is there an easy way to get a pointer or a handle onto the main form i have derived fromSystem.Windows.Forms.Form
to call its functions or get/set its variables? thanks in advance for any pointers! EDIT: just to clarify, i want to be able to call functions from anywhere in the program to set the status bar text, or to enable/disable a toolbar button, or to replace the form displayed in the background, etc. -
i am a newbie at c#, but have a good amount of time with vc++ development.. can someone help me with some questions? in vc++ my application could have variables and functions like:
CCompanyStore* myCompanyStore;
void ShowAboutDialog();and i would call them throughout the program like:
CMyApp* pApp = (CMyApp*)AfxGetApp();
pApp->ShowAboutDialog();i would use these types of functions for replacing views in my mainframe and such as well.. but i dont understand how to call a function from my base form without doing something stupid like calling
this.Parent.Parent.SetStatusText("hello");
.. is there an easy way to get a pointer or a handle onto the main form i have derived fromSystem.Windows.Forms.Form
to call its functions or get/set its variables? thanks in advance for any pointers! EDIT: just to clarify, i want to be able to call functions from anywhere in the program to set the status bar text, or to enable/disable a toolbar button, or to replace the form displayed in the background, etc. -
i am a newbie at c#, but have a good amount of time with vc++ development.. can someone help me with some questions? in vc++ my application could have variables and functions like:
CCompanyStore* myCompanyStore;
void ShowAboutDialog();and i would call them throughout the program like:
CMyApp* pApp = (CMyApp*)AfxGetApp();
pApp->ShowAboutDialog();i would use these types of functions for replacing views in my mainframe and such as well.. but i dont understand how to call a function from my base form without doing something stupid like calling
this.Parent.Parent.SetStatusText("hello");
.. is there an easy way to get a pointer or a handle onto the main form i have derived fromSystem.Windows.Forms.Form
to call its functions or get/set its variables? thanks in advance for any pointers! EDIT: just to clarify, i want to be able to call functions from anywhere in the program to set the status bar text, or to enable/disable a toolbar button, or to replace the form displayed in the background, etc.The equivalent of AfxGetApp is the .NET Application class, which holds the ApplicationContext and the MainForm. Unfortunately, the ApplicationContext member is not visible from your own code (it's protected).
-
The equivalent of AfxGetApp is the .NET Application class, which holds the ApplicationContext and the MainForm. Unfortunately, the ApplicationContext member is not visible from your own code (it's protected).
-
i guess the static variables would work for maintaining application variables, but i still dont understand how i would access the variable of that class type if it is a member variable of my main form.. basicly if i have a function which sets what is being displayed in my main form i would want to be able to call that function from another form.. still a newb.. cut me some slack :P -dz
-
so is there another way to get a handle on the main form? still a newb.. cut me some slack :P -dz
dazinith wrote: so is there another way to get a handle on the main form? First of all, a form handle is a different thing. All forms expose the underlying WIN32 window handle through the this.Handle property, but that's a different matter. Beside navigating the parent hierarchy (as you pointed out in your original post), the most straight forward way to let a main form method be called from a child form is to pass a
this
reference to it when the child form is created. Passingthis
passes a reference to it, not a copy.