dmoney
dmoney

Reputation: 881

Nested If / Else

So I have a sub on a button click that does the following:

  1. If the text entry within a combo box (cmbServerInstall.Text) is blank, firstly it will force the user to make a selection before proceeding.
  2. Else, a string (strGameServer) is populated with the text within the combo box (cmbServerInstall.Text).
  3. From here, a MessageBox will then show with a Yes/No option, asking if the user wishes to proceed.

Here is where things are going wrong.

What I want to happen

  1. If the user selects yes, then I want to use another if/else to determine what was stored in the string strGameServer. Depending on what this is set to, it will launch one of two batch files (I understand the file paths are the same at the moment, I plan to update this at a later date).
  2. If the user selects no, I want it to remove the selection from the combobox cmbServerInstall.

What is happening as it stands

  1. Basically the shell command launches the batch file REGARDLESS of whether or not MsgBoxResult is Yes or No.

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

Answers (2)

Steve
Steve

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

NiKiZe
NiKiZe

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

Related Questions