| | |
moving between forms
Please support our VB.NET advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Jun 2009
Posts: 3
Reputation:
Solved Threads: 0
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:
Section of code from Form1:
i want to use this again in Form2 to move to form 3 but i don't know how.
please advise.
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)
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:
VB.NET Syntax (Toggle Plain Text)
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.
If you want to use the same procedure, pass a parameter to proc to tell which form to show
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
VB.NET Syntax (Toggle Plain Text)
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
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
Teme64 @ Windows Developer Blog
•
•
Join Date: Jun 2009
Posts: 3
Reputation:
Solved Threads: 0
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.
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
I also added
VB.NET Syntax (Toggle Plain Text)
' 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
If <form> IsNot Nothing Then check just in case
Teme64 @ Windows Developer Blog
![]() |
Similar Threads
- DirectX in Visual C++: Help moving a mesh starting from BasicHLSL Tutorial (C++)
- What return before moving on? (Promotion and Marketing Plans)
- Forms in Random access files (Visual Basic 4 / 5 / 6)
- Snapping Forms together (VB.NET)
- Getting into a locked-down MS Access mdb file (Visual Basic 4 / 5 / 6)
- moving a form with no border (VB.NET)
- Moving to a larger case (Troubleshooting Dead Machines)
Other Threads in the VB.NET Forum
- Previous Thread: listview
- Next Thread: windows Login
Views: 396 | Replies: 4
| Thread Tools | Search this Thread |
Tag cloud for VB.NET
"crystal .net .net2005 2008 access add application array assignment basic box button buttons center class click code combo convert cpu data database datagrid datagridview design designer dissertation dissertations dissertationthesis dosconsolevb.net editvb.net employees error excel exists firewall function image images isnumericfuntioncall listview login map math memory mobile module msaccess mssqlbackend mysql navigate net opacity page pan picturebox port print printing printpreview problem record refresh regex reports" reuse right-to-left save savedialog search serial socket sorting sql sqldatbase storedprocedure string structures studio temp textbox timer txttoxmlconverter upload useraccounts usercontol usercontrol vb vb.net vb.nettoolboxvisualbasic2008sidebar vb2008 vbnet vista visual visualbasic visualbasic.net visualstudio2008 web wpf xml





