hey,
i am creating a basic game, something a bit like space invaders but only 3 levels long.

I'm haveing trouble moving from Form2 (level 2) to Form3 (level 3), Form3 opens, Form2 closes, and I want the start menu (which is on another form called Start) to show if one of the forms closes, but i don't want it poping up infront of the game when ever i move onto the next level, which is what it does.

I want to show the start form because if the user closes a form they should be led back to the start menu.

i have used this code on Form1 (level 1) to move onto Form2 but i don't know how to get it to work for moving from Form2 to Form3:

section of code from Start form:

Public Sub goto2()
        frmLevel1.Close()
        Dim frmLevel2 As Form2
        frmLevel2 = New Form2
        frmLevel2.Activate()
        frmLevel2.Show()
        Me.Hide()
    End Sub

Section of code from Form1:

If MessageBox.Show("Play Next Level?", "Yes/No question", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) = Windows.Forms.DialogResult.No Then
                MsgBox("Okay, Bye")
                Start.Show()
                'Me.Enabled = False
                Close()
            Else
                MsgBox("Okay! Good Luck")
                Start.goto2()
            End If

i want to use this again in Form2 to move to form 3 but i don't know how.

please advise.

Recommended Answers

All 4 Replies

If you want to use the same procedure, pass a parameter to proc to tell which form to show

Public Sub GotoForm(ByVal FormIndex As Integer)
  ' Change form
  Select Case FormIndex
    Case 2
      frmLevel1.Close()
      Dim frmLevel2 As Form2
      frmLevel2 = New Form2
      frmLevel2.Activate()
      frmLevel2.Show()
      Me.Hide()
    Case 3
      frmLevel2.Close()
      Dim frmLevel3 As Form3
      frmLevel3 = New Form3
      frmLevel3.Activate()
      frmLevel3.Show()
      Me.Hide()
    Case Else
      ' Wrong index
  End Select

End Sub

and call it Start.GotoForm(2), Start.GotoForm(3) etc.

Without knowing more about your code I can't guarantee it works "as is", but I hope it gives you an idea how to do it.

HTH

i get the idea and the code you replied was almost perfect except for 1 thing, i dosn't like closing form 2 for some reason.

Case (3)
frmLevel2.Close()
Dim frmLevel3 As Form3
frmLevel3 = New Form3
frmLevel3.Activate()
frmLevel3.Show()
Me.Hide()
End Select


it pops up the nullreferance unhandled box:
Object reference not set to an instance of an object.

what does this mean and how can i fix it?

thanks for your help.

Ok. Try to declare form variables global in your main form (Start form) i.e. where you have GotoForm. If you declare them inside the sub their scope will be only that sub and you "lose" the form reference when you exit the sub

' Globals in Start form
Private frmLevel1 As Form
Private frmLevel2 As Form
Private frmLevel3 As Form
.
.
Public Sub GotoForm(ByVal FormIndex As Integer)
  ' Change form
  Select Case FormIndex
    Case 2
      ' Additional check to avoid null reference error
      If frmLevel1 IsNot Nothing Then
        frmLevel1.Close()
      End If
      frmLevel2 = New Form2
      frmLevel2.Activate()
      frmLevel2.Show()
      Me.Hide()
    Case 3
      ' Additional check to avoid null reference error
      If frmLevel2 IsNot Nothing Then
        frmLevel2.Close()
      End If
      frmLevel3 = New Form3
      frmLevel3.Activate()
      frmLevel3.Show()
      Me.Hide()
    Case Else
      ' Wrong index
  End Select

End Sub

I also added If <form> IsNot Nothing Then check just in case :)

Thanks alot that works perfectly!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.