Reputation: 881
So I have a sub on a button click that does the following:
Here is where things are going wrong.
What I want to happen
What is happening as it stands
Could anyone kindly take a look at the code below and point me in the direction of where I am going wrong? Nested IFs seem to be getting the better of me.
Dim strGameServer As String
If cmbServerInstall.Text = "" Then
MessageBox.Show("Please select a game server to install", "No game server selected", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1)
Else
strGameServer = cmbServerInstall.Text
MessageBox.Show("You have chosen" + " " + strGameServer + "." + " " + "Please confirm you wish to proceed with your selection.", "Confirm game server selection", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1)
If MsgBoxResult.Yes Then
If strGameServer = "Counter-Strike: Global Offensive" Then
Shell("C:\Users\Damon\Desktop\YorkshaLAN Server Creator\YorkshaLAN Server Setup.bat", AppWinStyle.NormalFocus)
Else : strGameServer = "Team Fortress 2"
Shell("C:\Users\Damon\Desktop\YorkshaLAN Server Creator\YorkshaLAN Server Setup.bat", AppWinStyle.NormalFocus)
End If
Else
cmbServerInstall.Text = ""
End If
cmbServerInstall.Text = ""
cmbServerInstall.Enabled = False
btnServerGoInstall.Enabled = False
End If
End Sub
Upvotes: 1
Views: 3181
Reputation: 216273
You need to get the result of the MessageBox with the question and check the result
Dim result = MessageBox.Show("You have chosen ......")
If result = MsgBoxResult.Yes Then
.....
Actually your code checks the enum MsgBoxResult.Yes and because it is not zero the if is always evaulated as true
Also, if I were you I would try to remove any usage of the old VB6 syntax and enumerations. Actually MessageBox.Show returns a DialogResult enumeration not a MsgBoxResult. This is here just for VB6 compatibility
Dim result = MessageBox.Show("You have chosen ......")
If result = DialogResult.Yes Then
Upvotes: 0
Reputation: 1432
You need to save the result from MessageBox.Show and then check it, or do so in one line.
Edit of original code:
If cmbServerInstall.Text = "" Then
MessageBox.Show("Please select a game server to install", "No game server selected", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1)
Else
Dim strGameServer As String = cmbServerInstall.Text ' Moved init to avoid declaration without use '
If MessageBox.Show("You have chosen" & " " & strGameServer & "." & " " & "Please confirm you wish to proceed with your selection.",
"Confirm game server selection",
MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) =MsgBoxResult.Yes Then
If strGameServer = "Counter-Strike: Global Offensive" Then
Shell("C:\Users\Damon\Desktop\YorkshaLAN Server Creator\YorkshaLAN Server Setup.bat", AppWinStyle.NormalFocus)
Else
strGameServer = "Team Fortress 2"
Shell("C:\Users\Damon\Desktop\YorkshaLAN Server Creator\YorkshaLAN Server Setup.bat", AppWinStyle.NormalFocus)
End If
Else
cmbServerInstall.Text = ""
End If
cmbServerInstall.Text = ""
cmbServerInstall.Enabled = False
btnServerGoInstall.Enabled = False
End If
End Sub
Upvotes: 1