Vb消息框单击否并保留文本属性

时间:2014-01-10 16:27:14

标签: vb.net visual-studio-2010 messagebox

我有一个清除所有文本和组合框字段的按钮,我想给用户一个是或否的选项,以清除所有字段,并且不按照单击按钮时的方式保留它们。我有yes选项的代码我只需要没有部分。感谢。

 Private Sub btnNewForm_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNewForm.Click

    MsgBox("Are you sure you would like to clear the form", MsgBoxStyle.YesNo, "Confirm Delete")

    If MsgBoxResult.Yes Then


            For Each ctrl In Controls
                If TypeOf ctrl Is TextBox Or TypeOf ctrl Is ComboBox Then
                    ctrl.Text = String.Empty
                End If
            Next

    ElseIf MsgBoxResult.No Then

     ??????????????????

    End If

End Sub

2 个答案:

答案 0 :(得分:4)

您需要检查MessageBox的结果:

Dim results As DialogResult = MsgBox("Are you sure you would like to clear the form", MsgBoxStyle.YesNo, "Confirm Delete")

If results = DialogResult.Yes Then
  For Each ctrl In Controls
    If TypeOf ctrl Is TextBox Or TypeOf ctrl Is ComboBox Then
      ctrl.Text = String.Empty
    End If
  Next
End If

没有理由为“否”做任何事情,因为你不想改变与答案有关的任何内容。

答案 1 :(得分:0)

您没有正确使用它。你实际在做什么是....:将MsgboxResult.Yes与True进行比较。 请注意,MsgBox()实际上是一个返回用户单击按钮的函数。

Dim res as MsgBoxResult = MsgBox(...)
If res = MsgBoxResult.Yes then
  'Code for Yes
Else
  'Code for No
Endif

MsgBox()函数也是传统的VB6东西。最好使用

Dim res as DialogResult = MessageBox.Show("Message", "Title", ...)