[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 153
  • Last Modified:

Swiching between forms

I have a main menu and depending upon the selection,I am opening a new form ( by hiding the mainmenu) that has SUBMIT and CANCEL buttons.When the user presses CANCEL, the current form needs to be closed and the previous form needs to be displayed.Can you please give me an example how to do this as I am new to VB.NET.Thanks for your help.

Thanks
0
g46905
Asked:
g46905
1 Solution
 
TorrwinCommented:
To close the second form:
Me.Close()

To open the first form:
Dim frmFirstFormInstance As New frmFirstForm
frmFirstFormInstance.Show()
0
 
g46905Author Commented:
When I use it in this order, the current form is being closed and does the not display the first form.Any idea why this is happening

Me.Close()
Dim frmFirstFormInstance As New frmFirstForm
frmFirstFormInstance.Show()


thanks for your help
0
 
S-TwilleyCommented:
I think Torrwin meant for the

Me.Close()  

to be in the cancel button of the 2nd form (which was displayed on the menu item)

PS... don't give me credit
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
g46905Author Commented:
That is what I have done earlier.Any suggestions?When I did this, the application closes and does not open the form that is being called.

(to be in the cancel button of the 2nd form )

Thanks
0
 
S-TwilleyCommented:
ok, i think you're doing things slightly wrong

In your main/first form

Sub mnuOpenForm_Click(sender as object, e as eventargs) Handles mnuOpenForm.Click
    Dim frmFirstFormInstance As New frmFirstForm
    frmFirstFormInstance.Show()  ' or you might want to usefrmFirstFormInstance.ShowDialog()  
End Sub

========================

in you two-buttoned form.

Sub cmdClose_Click(sender as object, e as eventargs) Handles cmdClose.Click
    Me.Close()
End Sub
0
 
g46905Author Commented:
I believe that would close the current form, but how would if take me the previous form ( main menu )?
0
 
S-TwilleyCommented:
if you use .ShowDialog... it shows your two-buttoned form modally, which means you can't switch back to the other form till the current form (i.e. the two-buttoned one), is closed... once it's closed, it takes you back to the previously selected form which is you main menu form.

If you don't want to use ShowDialog, and on closing the two-button form, you wantto bring your main menu form to the front, you have to pass a reference to your two-button form of your other form so you can bring it to the front... sounds confusing i know... just try it with showdialog to see if that meets your needs first
0
 
g46905Author Commented:
I have tried showdialog.it display the main menu ( previous form ) buy my form two is not closed.

Thanks,
0
 
S-TwilleyCommented:
Ok... what is the name of the form with the main menu???

What is the name of hte form with two buttons???


My understanding is...  the main menu form is open...  what do you then click / do   to open up your two buttoned form (by that i mean, click on a button or something, i dont want to know the code) .....  then after that, the 2 buttoned form is shown... do you hide the main menu form while the 2 buttoned form is visible?

when you click cancel on the 2 buttons form, it goes back to the main menu form... is that correct
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Here is an alternate method to switching between forms...

The approach in the link below COMPLETELY closes out one form and opens another:
http://www.experts-exchange.com/Programming/Programming_Languages/Dot_Net/VB_DOT_NET/Q_21185658.html

Below is a modification of that code that simply hides the previous form.  It can be extended to handle any number of forms (this example just has Form1 and Form2).

--> In the Module, you add all of your forms to the MyForm2 Enum.
--> In the MyApplicationContext class, you add all of your forms to the private declarations area and then a matching area in the Select Case statement following the pattern I have shown.
--> All of your forms must expose the switchTo() event as shown in the examples.
--> Your project properties should be change to start with Sub Main()

' ---------------------------------------------------------------------------------------------------------------------------

Public Module MyApplication

    ' add all of your forms to the enum below
    Public Enum MyForms
        Form1 = 1
        Form2 = 2
    End Enum

    Public Sub Main()
        Dim context As MyApplicationContext = New MyApplicationContext
        Application.Run(context)
    End Sub

End Module

' ---------------------------------------------------------------------------------------------------------------------------

Public Class MyApplicationContext
    Inherits ApplicationContext

    ' add all of your forms here...
    Private frm1 As Form1
    Private frm2 As Form2

    Public Sub New()
        MyBase.New()
        ' Form1 will be the starting form
        switchTo(Nothing, MyApplication.MyForms.Form1)
    End Sub

    Private Sub switchTo(ByVal sender As Form, ByVal formName As MyForms)
        If Not (sender Is Nothing) Then
            ' hide whomever we just came from
            sender.Hide()
        End If

        ' switch to the requested from...creating a new instance if necessary
        Select Case formName
            Case MyApplication.MyForms.Form1
                If frm1 Is Nothing Then
                    frm1 = New Form1
                End If
                AddHandler frm1.switchTo, AddressOf switchTo
                AddHandler frm1.Closed, AddressOf OnFormClosed
                frm1.Show()

            Case MyApplication.MyForms.Form2
                If frm2 Is Nothing Then
                    frm2 = New Form2
                End If
                AddHandler frm2.switchTo, AddressOf switchTo
                AddHandler frm2.Closed, AddressOf OnFormClosed
                frm2.Show()

                ' add your other forms as seperate case statements here...

        End Select
    End Sub

    Private Sub OnFormClosed(ByVal sender As Object, ByVal e As EventArgs)
        ' if any of the forms are closed...then the application will close
        ExitThread()
    End Sub

End Class

' ---------------------------------------------------------------------------------------------------------------------------

Public Class Form1
    Inherits System.Windows.Forms.Form

    ' all of your forms must have the event below
    Public Event switchTo(ByVal sender As Form, ByVal formName As MyForms)

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        ' switch to whatever form is in the second parameter
        RaiseEvent switchTo(Me, MyApplication.MyForms.Form2)
    End Sub

End Class

' ---------------------------------------------------------------------------------------------------------------------------

Public Class Form2
    Inherits System.Windows.Forms.Form

    ' all of your forms must have the event below
    Public Event switchTo(ByVal sender As Form, ByVal formName As MyForms)

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        ' switch to whatever form is in the second parameter
        RaiseEvent switchTo(Me, MyApplication.MyForms.Form1)
    End Sub

End Class
0
 
S-TwilleyCommented:
All looks interesting... might have to read that later... im about to crash, so I'll leave the Qs in your capable hands Idle
0
 
S-TwilleyCommented:
... and by crash i mean fall asleep (not the computer kind of crash :P)
0
 
g46905Author Commented:
Idle Mind,
              I am relatively new to VB.NET and I was hoping if you could help me with the changes I need to make in the code.I have attached both the forms ( 1 & 2 ).thanks for your help

' Form 1 code >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

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 baddUser As System.Windows.Forms.Button
    Friend WithEvents baddProject As System.Windows.Forms.Button
    Friend WithEvents bassignUser As System.Windows.Forms.Button
    Friend WithEvents bassignProject As System.Windows.Forms.Button
    Friend WithEvents lwpAdmin As System.Windows.Forms.Label
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.baddUser = New System.Windows.Forms.Button
        Me.baddProject = New System.Windows.Forms.Button
        Me.bassignUser = New System.Windows.Forms.Button
        Me.bassignProject = New System.Windows.Forms.Button
        Me.lwpAdmin = New System.Windows.Forms.Label
        Me.SuspendLayout()
        '
        'baddUser
        '
        Me.baddUser.Location = New System.Drawing.Point(80, 50)
        Me.baddUser.Name = "baddUser"
        Me.baddUser.Size = New System.Drawing.Size(125, 34)
        Me.baddUser.TabIndex = 0
        Me.baddUser.Text = "AddUser"
        '
        'baddProject
        '
        Me.baddProject.Location = New System.Drawing.Point(80, 100)
        Me.baddProject.Name = "baddProject"
        Me.baddProject.Size = New System.Drawing.Size(125, 35)
        Me.baddProject.TabIndex = 1
        Me.baddProject.Text = "AddProject"
        '
        'bassignUser
        '
        Me.bassignUser.Location = New System.Drawing.Point(80, 150)
        Me.bassignUser.Name = "bassignUser"
        Me.bassignUser.Size = New System.Drawing.Size(125, 35)
        Me.bassignUser.TabIndex = 2
        Me.bassignUser.Text = "Assign User "
        '
        'bassignProject
        '
        Me.bassignProject.Location = New System.Drawing.Point(80, 200)
        Me.bassignProject.Name = "bassignProject"
        Me.bassignProject.Size = New System.Drawing.Size(125, 35)
        Me.bassignProject.TabIndex = 3
        Me.bassignProject.Text = "AssignProject"
        '
        'lwpAdmin
        '
        Me.lwpAdmin.Location = New System.Drawing.Point(64, 0)
        Me.lwpAdmin.Name = "lwpAdmin"
        Me.lwpAdmin.Size = New System.Drawing.Size(168, 23)
        Me.lwpAdmin.TabIndex = 4
        Me.lwpAdmin.Text = "Web Promotion Administration"
        Me.lwpAdmin.TextAlign = System.Drawing.ContentAlignment.TopCenter
        '
        'StartDialog
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(292, 266)
        Me.Controls.Add(Me.lwpAdmin)
        Me.Controls.Add(Me.bassignProject)
        Me.Controls.Add(Me.bassignUser)
        Me.Controls.Add(Me.baddProject)
        Me.Controls.Add(Me.baddUser)
        Me.Name = "StartDialog"
        Me.Text = "StartDialog"
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private Sub baddUser_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles baddUser.Click
        Me.Hide()
        Dim addUsr As New AddUser
        addUsr.ShowDialog()
    End Sub

    Private Sub baddProject_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles baddProject.Click
        Me.Hide()
        Dim addPrjt As New AddUser
        addPrjt.ShowDialog()
    End Sub

    Private Sub bassignUser_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bassignUser.Click
        Me.Hide()
        Dim asnUsr2Prjt As New AddUser
        asnUsr2Prjt.ShowDialog()
    End Sub
End Class


' From 2 Code >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

Public Class AddUser
    Inherits System.Windows.Forms.Form

#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 userID As System.Windows.Forms.Label
    Friend WithEvents role As System.Windows.Forms.Label
    Friend WithEvents submit As System.Windows.Forms.Button
    Friend WithEvents cancel As System.Windows.Forms.Button
    Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
    Friend WithEvents Label1 As System.Windows.Forms.Label
    Friend WithEvents ComboBox1 As System.Windows.Forms.ComboBox
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.userID = New System.Windows.Forms.Label
        Me.role = New System.Windows.Forms.Label
        Me.submit = New System.Windows.Forms.Button
        Me.cancel = New System.Windows.Forms.Button
        Me.TextBox1 = New System.Windows.Forms.TextBox
        Me.Label1 = New System.Windows.Forms.Label
        Me.ComboBox1 = New System.Windows.Forms.ComboBox
        Me.SuspendLayout()
        '
        'userID
        '
        Me.userID.Font = New System.Drawing.Font("Microsoft Sans Serif", 9.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.userID.Location = New System.Drawing.Point(32, 56)
        Me.userID.Name = "userID"
        Me.userID.RightToLeft = System.Windows.Forms.RightToLeft.No
        Me.userID.Size = New System.Drawing.Size(75, 23)
        Me.userID.TabIndex = 0
        Me.userID.Text = "User ID"
        '
        'role
        '
        Me.role.Font = New System.Drawing.Font("Microsoft Sans Serif", 9.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.role.Location = New System.Drawing.Point(32, 104)
        Me.role.Name = "role"
        Me.role.RightToLeft = System.Windows.Forms.RightToLeft.No
        Me.role.Size = New System.Drawing.Size(75, 23)
        Me.role.TabIndex = 1
        Me.role.Text = "Role"
        '
        'submit
        '
        Me.submit.Location = New System.Drawing.Point(32, 168)
        Me.submit.Name = "submit"
        Me.submit.TabIndex = 2
        Me.submit.Text = "Submit"
        '
        'cancel
        '
        Me.cancel.Location = New System.Drawing.Point(144, 168)
        Me.cancel.Name = "cancel"
        Me.cancel.TabIndex = 3
        Me.cancel.Text = "Cancel"
        '
        'TextBox1
        '
        Me.TextBox1.Font = New System.Drawing.Font("Microsoft Sans Serif", 9.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.TextBox1.Location = New System.Drawing.Point(144, 56)
        Me.TextBox1.Name = "TextBox1"
        Me.TextBox1.TabIndex = 4
        Me.TextBox1.Text = "TextBox1"
        '
        'Label1
        '
        Me.Label1.Font = New System.Drawing.Font("Microsoft Sans Serif", 9.5!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.Label1.ImageAlign = System.Drawing.ContentAlignment.TopCenter
        Me.Label1.Location = New System.Drawing.Point(120, 0)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(128, 32)
        Me.Label1.TabIndex = 6
        Me.Label1.Text = "User Administration"
        Me.Label1.TextAlign = System.Drawing.ContentAlignment.TopCenter
        '
        'ComboBox1
        '
        Me.ComboBox1.Font = New System.Drawing.Font("Microsoft Sans Serif", 9.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.ComboBox1.Location = New System.Drawing.Point(144, 104)
        Me.ComboBox1.Name = "ComboBox1"
        Me.ComboBox1.Size = New System.Drawing.Size(120, 23)
        Me.ComboBox1.TabIndex = 7
        Me.ComboBox1.Text = "ComboBox1"
        '
        'AddUser
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(392, 266)
        Me.Controls.Add(Me.ComboBox1)
        Me.Controls.Add(Me.Label1)
        Me.Controls.Add(Me.TextBox1)
        Me.Controls.Add(Me.cancel)
        Me.Controls.Add(Me.submit)
        Me.Controls.Add(Me.role)
        Me.Controls.Add(Me.userID)
        Me.Name = "AddUser"
        Me.Text = "AddUser"
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private Sub cancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cancel.Click
        Me.Close()
        Dim frmFirstFormInstance As New StartDialog
        frmFirstFormInstance.ShowDialog()
    End Sub
End Class











0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now