I'm trying to guide the user during a login Form.
If the user clicks Yes
and they have entered a nickname, then the next Form will start and the current one close.
If they select Yes
without entering a nickname, then a message will appear to inform that the username is missing and no other Form is shown.
If they select No
, then the MessageBox should just close.
This is the code I came up with:
Public Sub BttPlay_Click(sender As Object, e As EventArgs) Handles BttPlay.Click
Select Case MsgBox("The game is going to start, are you ready?", MessageBoxButtons.YesNo) And TxtUsername.Text = ""
Case DialogResult.Yes And TxtUsername.Text <> ""
nivel1.Show()
Me.Close()
Case DialogResult.Yes And TxtUsername.Text = ""
MsgBox("You must write your username", MessageBoxButtons.OK)
Case DialogResult.No
Me.Show()
End Select
Me.Refresh()
End Sub
The problem is that it's showing the message to enter a nickname no matter if one has already been entered or not. What am I doing wrong?
Since the procedure cannot go any further if the User has not entered a name, check this condition first, then notify and return if this condition is not met.
Then ask whether the User wants to start a new game (could be redundant, since the User has already entered a Name and pressed the Button).
If the User answers No
, so you get DialogResult.No
as result, the just Return
, there's nothing else to do here.
Otherwise, show the nivel1
Form and eventually close the current Form (see the footer note):
Public Sub BttPlay_Click(sender As Object, e As EventArgs) Handles BttPlay.Click
If TxtUsername.TextLength = 0 Then
MessageBox.Show("Please, name", "Name")
Return
End If
If MessageBox.Show("Start?", "New Game", MessageBoxButtons.YesNo) = DialogResult.No Then Return
nivel1.Show()
Me.Close()
End Sub
If you need to use Select Case
for some reason, you can use the result of the MessageBox as the selector. Since you probably are just interested in a positive result, you can check that case first and leave the rest to Case Else
(since there are just two possible cases):
Public Sub BttPlay_Click(sender As Object, e As EventArgs) Handles BttPlay.Click
If txtDescription.TextLength = 0 Then
MessageBox.Show("Please, name", "Name")
Return
End If
Select Case MessageBox.Show("Start?", "New Game", MessageBoxButtons.YesNo)
Case DialogResult.Yes
nivel1.Show()
Me.Close()
Case Else
' NOP - It will just exit this method
' Me.Show() is not required, since Me is already shown
End Select
End Sub
Note that if Me
is the starting Form, Me.Close()
will end the program.
You probably don't want to close the program here, so make sure that the Shutdown mode
option in Project->Properties->Application
is set to When last Form closes
.
You can use Me.Hide()
as an alternative.
Since you're learning the language, I suggest to set Option Strict On
from the beginning. It will help you a lot to avoid mistakes that can be difficult to spot after you have written a bunch of code.
Visual Studio, the debugger and Intellisense will all help to find out what's wrong with the code an also suggest how to fix it.
You case set this option On
(along with al the other related options),
using the Visual Studio general Options dialog.
In the TOOLS
menu, see: Options->Projects and Solutions->VB Defaults
and set all to On
.