mwalth
mwalth

Reputation: 113

Trouble switching between projects/forms in vb.net

I am working on a project in Visual Studio Community 2019 using VB.Net. I am developing a user interface wherein users start on one form (a sort of index, call it form 1) and select from their the next form they want to go to (say form 2). This part is working fine. The trouble is that after users finish in form 2, they should be directed back to form 1 to continue from here, and I can't get this to happen. I need somehow to make the main menu a public shared form so that the other projects can access it but I can’t figure out how to do that.

Here are the relevant lines from form 1:

    Public Class frmMainMenu
    Public Shared inst As frmMainMenu
    Private Sub frmMenu_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)  Handles MyBase.Load
       inst = Me
       Timer1.Start()
       Me.Show()
    End Sub

 Private Sub BtnGeology_Click(sender As Object, e As EventArgs) Handles 
  btnGeology.Click
        Dim formNew As New Geology.frmGeologyMenu
        formNew.Show()
        Timer1.Stop()
        Me.Hide()
    End Sub

And here are the relevant lines from form 2. I want lines 23 and 24 to return the user to the main menu, but it doesn't work.

Public Class frmGeologyMenu
    Public Sub frmGeologyMenu_Load(ByVal sender As System.Object, ByVal e As 
  System.EventArgs) Handles MyBase.Load
        lblTime.Text = 10
        Me.Show
    End Sub
 Private Sub BtnMainMenu_Click(sender As Object, e As EventArgs) Handles 
  btnMainMenu.Click
        inst.frmMainMenu.Show()
        inst.frmMainMenu.Timer1.Start()
        Me.Close()
    End Sub

I appreciate any help you can offer!

Upvotes: 1

Views: 76

Answers (2)

Idle_Mind
Idle_Mind

Reputation: 39142

From my comments above:

When you create your instance of your second form called "formNew", use the AddHandler command to wire up its FormClosed event to a method in the main form. Then in the second form you can just call Me.Close() and the method in the main form will fire where you can call Me.Show()..

In main menu:

Public Class frmMainMenu

    Private Sub BtnGeology_Click(sender As Object, e As EventArgs) Handles btnGeology.Click
        Dim formNew As New Geology.frmGeologyMenu
        AddHandler formNew.FormClosed, AddressOf formNew_FormClosed
        Timer1.Stop()
        Me.Hide()            
        formNew.Show()                        
    End Sub

    Private Sub formNew_FormClosed(sender As Object, e As FormClosedEventArgs)
        Me.Show()
        Timer1.Start()
    End Sub

End Class

In frmGeologyMenu:

Public Class frmGeologyMenu

    Public Sub frmGeologyMenu_Load(ByVal sender As System.Object, ByVal e As 
  System.EventArgs) Handles MyBase.Load
        lblTime.Text = 10
    End Sub

    Private Sub BtnMainMenu_Click(sender As Object, e As EventArgs) Handles btnMainMenu.Click
        Me.Close()
    End Sub

End Class

Upvotes: 1

jmcilhinney
jmcilhinney

Reputation: 54457

In VB.NET, your startup form is always the default instance of its type. That means that you can simply use the type name to refer to your startup form anywhere in your app. If you use Me.Hide() in your startup form then frmMainMenu.Show() can be used anywhere to redisplay it. This would not be the case in C# as default form instances are a VB-specific feature. See here for more info.

Upvotes: 1

Related Questions