VB.NET - MessageBox YesNo 条件语句不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25610408/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 18:08:50  来源:igfitidea点击:

VB.NET - MessageBox YesNo Conditional Statement not working

vb.net

提问by user2308700

I have a program where I am clearing out comboboxes that the user is using to answer multiple choice questions. If the user clicks Clear, a messagebox pops up and asks the user if they are sure they want to clear out the form. If they press yes, it clears out all comboboxes. As of right now, if the user presses no, it still clears out the comboboxes.

我有一个程序,我正在清除用户用来回答多项选择问题的组合框。如果用户单击清除,则会弹出一个消息框并询问用户是否确定要清除表单。如果他们按是,它会清除所有组合框。截至目前,如果用户按“否”,它仍会清除组合框。

Code:

代码:

  Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click

    MessageBox.Show("Are you sure you want to clear your answers?", "Attention!",             MessageBoxButtons.YesNo)
    If Windows.Forms.DialogResult.Yes Then
        For Each cbo As ComboBox In Controls.OfType(Of ComboBox)()
            cbo.Items.Clear()
        Next
    End If


End Sub

回答by ??ssa P?ngj?rdenlarp

You need to capture the MessageBox result:

您需要捕获 MessageBox 结果:

Dim dlgR as DialogResult

DlgR = MessageBox.Show("Are you sure you want to clear your answers?", 
            "Attention!", MessageBoxButtons.YesNo)

' then test it:
If dlgR = DialogResult.Yes Then
    For Each cbo As ComboBox In Controls.OfType(Of ComboBox)()
        cbo.Items.Clear()
    Next
End If

MessageBoxis a function which returns a DialogResult. Your code

MessageBox是一个返回 a 的函数DialogResult。你的代码

If Windows.Forms.DialogResult.Yes Then...

isnt evaluating the user's response; DialogResult.Yesis not False (0), so it will always be True and cause the CBOs to be cleared no matter what they answer.

不评估用户的反应;DialogResult.Yes不是 False (0),因此它将始终为 True 并导致 CBO 无论他们回答什么都会被清除。

Also, that code is not legal under Option Strict. VB/VS would inform you of this and prompt you to change it if Option Strictis on; this will avoid many similar - and more serious - errors elsewhere.

此外,该代码在Option Strict. VB/VS 会通知您这一点并提示您更改它(如果Option Strict打开);这将避免在其他地方出现许多类似的——更严重的——错误。

回答by user2308700

You can use the following method also:

您也可以使用以下方法:

    If MsgBox("Are you sure you want to clear your answers?", MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
        cbo.Items.Clear()
    Else
        'false condition to be executed
    End If

Note: To clear combo box items you need not to use a iterative loop cbo.Items.Clear()will clear the item list of combo box

注意:要清除组合框项目,您不需要使用迭代循环 cbo.Items.Clear()将清除组合框的项目列表

If you want to remove a particular item from you can use two methods

如果要从中删除特定项目,可以使用两种方法

  • Based on the item

    cbo.Items.Remove("Item As string")

  • 基于 item

    cbo.Items.Remove("Item As string")

Example:cbo.Items.Remove("Apple")

例子:cbo.Items.Remove("Apple")

  • Based on Index

    cbo.Items.RemoveAt(Index As Integer)

  • 基于 Index

    cbo.Items.RemoveAt(Index As Integer)

Example:cbo.Items.RemoveAt(2)

例子:cbo.Items.RemoveAt(2)