Providing a reference to an application's main form
Providing a reference to an application's main form
<URL: http://dotnet.mvps.org/dotnet/faqs? Id = accessmainform & lang = en>
----------------------------------------------------------------------------
Providing a reference to an application's main form
Let 'mainform' be the class name of the application's main form. Add
Code below to your project, then go to the Project Settings and change
Startup object to 'sub main ':
///
Public Module Program
Private m_mainform as mainform
Public readonly property mainform () as mainform
Get
Return m_mainform
End get
End Property
Public sub main ()
M_mainform = new mainform ()
Application. Run (m_mainform)
End sub
End Module
///
You can access the main form from everywhere in the app by typing
'Program. mainform '.
-Or-
///
Public class Program
Private shared m_mainform as mainform
Public shared readonly property mainform () as mainform
Get
Return m_mainform
End get
End Property
Public shared sub main ()
M_mainform = new mainform ()
Application. Run (m_mainform)
End sub
End Class
///
There are still some alternatives, for example, you can implement
Singleton Design Pattern for your forms in order to access them by their
Class Name:
Implementing the Singleton pattern in C #
<URL: http://www.yoda.arachsys.com/csharp/singleton.html>
Refreshing ing the singleton Design Pattern
<URL: http://msdn.microsoft.com/library/en-us/dnbda/html/singletondespatt.asp>
Design Pattern: Singleton in C #
<URL: http://www.devhood.com/tutorials/tutorial_details.aspx? Tutorial_id = 486>
Design Patterns: Singleton
<URL: http://www.dofactory.com/Patterns/PatternSingleton.aspx>
My practice is to declare a static class in the shared DLL, which has a static attribute to access and set mainform!