Skip Navigation Links.
VB.Net Example - How to show a form.
Language(s):VB.Net
Category(s):Winforms

Showing a form in VB 6.0 was as easy as just invoking the Show method. This example shows how to do the same thing with VB.Net.

'VB.Net Example - Showing a Windows Form
'
'(c) Jon Vote 07/2002
'Idioma Software Inc. 
'All rights reserved 
'
'www.idioma-software.com 
'jon@idioma-software.com 
'
'Code is presented as is. 
'Use at your own risk. 

'1) Start a new project. Form1 is created by default. 
'2) From the menu select Project|Add Windows Form.
'3) Click the Open button.
'4) Form2 should now be added to the project.
'5) Display Form1 by double-clicking the Form1 
'   icon in the Solution Explorer (normally 
'   located to the right.)
'6) Press F7 to bring up the code window.
'7) Replace all of the code for Form1 with 
'   the following: 
'VB.Net example 
'(c) Jon Vote 07/2002 
'All rights reserved.
'Idioma Software Inc. 
'www.idioma-software.com
'
'Code may be freely used for development 
'purposes only as long as credit is given 
'to the author. Unauthorized pulication is prohbited.
'
'Here is one way to display a form in VB.Net
'1) Declare WithEvents a modular variable of type form to display.
'2) Set the varible to Nothing in the Closed event for this variable.
'3) To display the form, first check to see if the variable in 
'   step 1 is Nothing. If so, set it to a new instance of the form
'   and then use the Show (or ShowDialog) method.
'4) If the variable is not Nothing, use the Activate method. 
'
Public Class Form1
    Inherits System.Windows.Forms.Form

    'Declare variable WithEvents as type Form2
    Private WithEvents m_frmForm2 As Form2
#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents cmdShowForm As System.Windows.Forms.Button
    Friend WithEvents chkModal As System.Windows.Forms.CheckBox
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.cmdShowForm = New System.Windows.Forms.Button()
        Me.chkModal = New System.Windows.Forms.CheckBox()
        Me.SuspendLayout()
        '
        'cmdShowForm
        '
        Me.cmdShowForm.Location = New System.Drawing.Point(10, 18)
        Me.cmdShowForm.Name = "cmdShowForm"
        Me.cmdShowForm.TabIndex = 0
        Me.cmdShowForm.Text = "Show Form"
        '
        'chkModal
        '
        Me.chkModal.Location = New System.Drawing.Point(90, 22)
        Me.chkModal.Name = "chkModal"
        Me.chkModal.Size = New System.Drawing.Size(152, 16)
        Me.chkModal.TabIndex = 1
        Me.chkModal.Text = "Show modally"
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(252, 57)
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.chkModal, Me.cmdShowForm})
        Me.MaximizeBox = False
        Me.Name = "Form1"
        Me.Text = "Presented by Idioma Software"
        Me.ResumeLayout(False)

    End Sub

#End Region

    'Set form variable to nothing when Form2 closed.
    Private Sub m_frmForm2_Closed(ByVal sender As Object, ByVal e As System.EventArgs) Handles m_frmForm2.Closed
        m_frmForm2 = Nothing
    End Sub

    'Show the form
    Private Sub cmdShowForm_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdShowForm.Click
        If m_frmForm2 Is Nothing Then
            m_frmForm2 = New Form2()
            If chkModal.Checked Then
                m_frmForm2.ShowDialog()
            Else
                m_frmForm2.Show()
            End If
        Else
            m_frmForm2.Activate()
        End If
    End Sub
End Class

This article has been viewed 3631 times.
The examples on this page are presented "as is". They may be used in code as long as credit is given to the original author. Contents of this page may not be reproduced or published in any other manner what so ever without written permission from Idioma Software Inc.