moving between forms

Please support our VB.NET advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Jun 2009
Posts: 3
Reputation: williamyounger is an unknown quantity at this point 
Solved Threads: 0
williamyounger williamyounger is offline Offline
Newbie Poster

moving between forms

 
0
  #1
Jun 16th, 2009
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:
  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:
  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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 710
Reputation: Teme64 will become famous soon enough Teme64 will become famous soon enough 
Solved Threads: 115
Teme64's Avatar
Teme64 Teme64 is offline Offline
Master Poster

Re: moving between forms

 
0
  #2
Jun 17th, 2009
If you want to use the same procedure, pass a parameter to proc to tell which form to show
  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
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 3
Reputation: williamyounger is an unknown quantity at this point 
Solved Threads: 0
williamyounger williamyounger is offline Offline
Newbie Poster

Re: moving between forms

 
0
  #3
Jun 17th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 710
Reputation: Teme64 will become famous soon enough Teme64 will become famous soon enough 
Solved Threads: 115
Teme64's Avatar
Teme64 Teme64 is offline Offline
Master Poster

Re: moving between forms

 
0
  #4
Jun 17th, 2009
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
  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
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 3
Reputation: williamyounger is an unknown quantity at this point 
Solved Threads: 0
williamyounger williamyounger is offline Offline
Newbie Poster

Re: moving between forms

 
0
  #5
Jun 18th, 2009
Thanks alot that works perfectly!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the VB.NET Forum


Views: 396 | Replies: 4
Thread Tools Search this Thread



Tag cloud for VB.NET
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC