943,569 Members | Top Members by Rank

Ad:
  • VB.NET Discussion Thread
  • Marked Solved
  • Views: 798
  • VB.NET RSS
Jun 16th, 2009
-1

moving between forms

Expand Post »
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:
VB.NET Syntax (Toggle Plain Text)
  1. Public Sub goto2()
  2. frmLevel1.Close()
  3. Dim frmLevel2 As Form2
  4. frmLevel2 = New Form2
  5. frmLevel2.Activate()
  6. frmLevel2.Show()
  7. Me.Hide()
  8. End Sub

Section of code from Form1:
VB.NET Syntax (Toggle Plain Text)
  1.  
  2. If MessageBox.Show("Play Next Level?", "Yes/No question", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) = Windows.Forms.DialogResult.No Then
  3. MsgBox("Okay, Bye")
  4. Start.Show()
  5. 'Me.Enabled = False
  6. Close()
  7. Else
  8. MsgBox("Okay! Good Luck")
  9. Start.goto2()
  10. End If

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

please advise.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
williamyounger is offline Offline
3 posts
since Jun 2009
Jun 17th, 2009
0

Re: moving between forms

If you want to use the same procedure, pass a parameter to proc to tell which form to show
VB.NET Syntax (Toggle Plain Text)
  1. Public Sub GotoForm(ByVal FormIndex As Integer)
  2. ' Change form
  3. Select Case FormIndex
  4. Case 2
  5. frmLevel1.Close()
  6. Dim frmLevel2 As Form2
  7. frmLevel2 = New Form2
  8. frmLevel2.Activate()
  9. frmLevel2.Show()
  10. Me.Hide()
  11. Case 3
  12. frmLevel2.Close()
  13. Dim frmLevel3 As Form3
  14. frmLevel3 = New Form3
  15. frmLevel3.Activate()
  16. frmLevel3.Show()
  17. Me.Hide()
  18. Case Else
  19. ' Wrong index
  20. End Select
  21.  
  22. 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
Reputation Points: 218
Solved Threads: 201
Veteran Poster
Teme64 is offline Offline
1,024 posts
since Aug 2008
Jun 17th, 2009
0

Re: moving between forms

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
williamyounger is offline Offline
3 posts
since Jun 2009
Jun 17th, 2009
0

Re: moving between forms

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
VB.NET Syntax (Toggle Plain Text)
  1. ' Globals in Start form
  2. Private frmLevel1 As Form
  3. Private frmLevel2 As Form
  4. Private frmLevel3 As Form
  5. .
  6. .
  7. Public Sub GotoForm(ByVal FormIndex As Integer)
  8. ' Change form
  9. Select Case FormIndex
  10. Case 2
  11. ' Additional check to avoid null reference error
  12. If frmLevel1 IsNot Nothing Then
  13. frmLevel1.Close()
  14. End If
  15. frmLevel2 = New Form2
  16. frmLevel2.Activate()
  17. frmLevel2.Show()
  18. Me.Hide()
  19. Case 3
  20. ' Additional check to avoid null reference error
  21. If frmLevel2 IsNot Nothing Then
  22. frmLevel2.Close()
  23. End If
  24. frmLevel3 = New Form3
  25. frmLevel3.Activate()
  26. frmLevel3.Show()
  27. Me.Hide()
  28. Case Else
  29. ' Wrong index
  30. End Select
  31.  
  32. End Sub
I also added If <form> IsNot Nothing Then check just in case
Reputation Points: 218
Solved Threads: 201
Veteran Poster
Teme64 is offline Offline
1,024 posts
since Aug 2008
Jun 18th, 2009
0

Re: moving between forms

Thanks alot that works perfectly!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
williamyounger is offline Offline
3 posts
since Jun 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in VB.NET Forum Timeline: listview
Next Thread in VB.NET Forum Timeline: windows Login





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC