嵌套If / Else

时间:2013-09-05 22:21:29

标签: vb.net if-statement nested

所以我点按了一个按钮,执行以下操作:

  1. 如果组合框(cmbServerInstall.Text)中的文本条目为空,则首先会强制用户在继续之前进行选择。
  2. 否则,将使用组合框(cmbServerInstall.Text)中的文本填充字符串(strGameServer)。
  3. 从这里开始,MessageBox将显示Yes / No选项,询问用户是否希望继续。
  4. 这是出问题的地方。

    我想要发生什么

    1. 如果用户选择yes,那么我想使用另一个if / else来确定字符串strGameServer中存储的内容。根据设置的内容,它将启动两个批处理文件中的一个(我知道文件路径目前是相同的,我计划在以后更新)。
    2. 如果用户选择“否”,我希望它从组合框cmbServerInstall中删除选择。
    3. 目前正在发生的事情

      1. 基本上,shell命令启动批处理文件REGARDLESS MsgBoxResult是否为是。
      2. 有人可以看看下面的代码并指出我出错的方向吗?嵌套的IF似乎越来越好。

            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
        

2 个答案:

答案 0 :(得分:1)

您需要从MessageBox.Show保存结果,然后检查它,或者在一行中执行此操作。

编辑原始代码:

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

答案 1 :(得分:0)

您需要使用问题获取MessageBox的结果并检查结果

   Dim result = MessageBox.Show("You have chosen ......")
   If result = MsgBoxResult.Yes Then
       .....

实际上你的代码会检查枚举MsgBoxResult.Yes,因为它不是零,if总是被证实是真的

另外,如果我是你,我会尝试删除旧的VB6语法和枚举的任何用法。实际上MessageBox.Show返回一个DialogResult枚举而不是MsgBoxResult。这仅适用于VB6兼容性

   Dim result = MessageBox.Show("You have chosen ......")
   If result = DialogResult.Yes Then