Providing a Reference to an Application’s Main Form

The sample below assumes that MainForm is the class name of the application’s main form. In the project settings Sub Main has to be selected as startup object:

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

Alternatively, the Singleton design pattern can be implemented for forms to enable access via their class names: