所以我点按了一个按钮,执行以下操作:
这是出问题的地方。
我想要发生什么
目前正在发生的事情
有人可以看看下面的代码并指出我出错的方向吗?嵌套的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
答案 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