1. Herfried K. Wagner’s VB.Any
  2. .NET
  3. Frequently Asked Questions

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 the
code below to your project, then go to the project settings and change the
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 the
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>

Exploring 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>