我想出了如何清除文本框,但是当我在MessageBox上按 No 时它会清除。我希望它清除用户是否选择是。如果用户选择否,那么我想什么都不做。
Private Sub BtnFah_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnFah.Click
Try
Dim intFah As Integer
intFah = CInt(TxtBoxTemp.Text)
intFah = (intFah * 9) / 5 - 32
If MessageBox.Show(intFah.ToString & ControlChars.CrLf & "Would you like to start another temp conversion?", "Result", MessageBoxButtons.YesNo) Then
TxtBoxTemp.Text = String.Empty
End If
Catch
MessageBox.Show("Would you like to start another temp conversion?", "System Error", MessageBoxButtons.YesNo)
End Try
End Sub
答案 0 :(得分:2)
您需要比较DialogResult
Public Class Form1
Private Sub BtnFah_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnFah.Click
Try
Dim intFah As Integer
intFah = CInt(TxtBoxTemp.Text)
intFah = (intFah * 9) / 5 - 32
If MessageBox.Show(intFah.ToString & ControlChars.CrLf & "Would you like to start another temp conversion?", "Result", MessageBoxButtons.YesNo) = DialogResult.Yes Then
TxtBoxTemp.Text = String.Empty
End If
Catch
MessageBox.Show("Would you like to start another temp conversion?", "System Error", MessageBoxButtons.YesNo)
End Try
End Sub
End Class
答案 1 :(得分:2)
MessageBox.Show
返回DialogResult
。您应该检查DialogResult.Yes
:
If MessageBox.Show(...) = DialogResult.Yes Then
TxtBoxTemp.Text = String.Empty
End If
答案 2 :(得分:0)
使用Q作为变量并且暗淡如msgboxresult使msgbox有结果,如果按yes,文本框将清除,如果没有,则它将返回或什么都不做
Dim q As MsgBoxResult
q = MsgBox("Your Question", vbYesNo)
If q = vbYes Then
TextBox1.Clear()
Else
Return
End If
答案 3 :(得分:0)
试试这个......
If MessageBox.Show("Your Message", "Title", MessageBoxButtons.YesNo) = DialogResult.Yes Then
textbox.clear()
End If