使用MessageBox yes按钮清除文本框



我知道如何清除文本框,但当我按No在MessageBox上清除。如果用户选择Yes,我希望清除它。如果用户选择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

您需要比较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

MessageBox.Show返回一个DialogResult。您应该检查DialogResult.Yes:

If MessageBox.Show(...) = DialogResult.Yes Then
   TxtBoxTemp.Text = String.Empty
End If

试试这个…

If MessageBox.Show("Your Message", "Title", MessageBoxButtons.YesNo) = DialogResult.Yes Then
textbox.clear()
End If

使用Q作为变量并dim为msgboxresult使msgbox具有结果,如果按yes,文本框将被清除,如果不按,则它将返回或不做任何事情

 Dim q As MsgBoxResult
            q = MsgBox("Your Question", vbYesNo)
            If q = vbYes Then
                TextBox1.Clear()
            Else
                Return
            End If

最新更新