在vb中弹出selectedindexchanged上的消息

时间:2014-07-21 11:18:34

标签: vb.net dropdownlistfor

我想知道每次下拉列表中的所选项目发生变化时,如何显示弹出警告消息。我设法让它工作,但不幸的是,在加载表单时也会弹出消息,因为在某些情况下默认值已经设置且不为空。到目前为止我有这个:

Private Sub cboNew_DropDown(sender As Object, e As EventArgs) Handles   cboNew.SelectedIndexChanged

    If cboNew.SelectedItem = "%" Then
        MessageBox.Show(String.Format(" You Selected = " & cboNew.SelectedItem, MessageBoxButtons.OK))
    End If

    If cboNew.SelectedItem = "Value" Then
        MessageBox.Show(String.Format(" You Selected = " & cboNew.SelectedItem, MessageBoxButtons.OK))
    End If

    If cboNew.SelectedItem = "REVOKE" Then
        MessageBox.Show(String.Format(" You Selected = " & cboNew.SelectedItem, MessageBoxButtons.OK))
    End If
End Sub

2 个答案:

答案 0 :(得分:0)

有时很难确定错误的来源。你可以添加一个布尔标志来帮助你。

Private _isLoading As Boolean = False

Private Sub YourLoadingSub
    _isLoading = True
    ' Do your loading code
    _isLoading = False
End Sub

Private Sub cboNew_DropDown(sender As Object, e As EventArgs) Handles   cboNew.SelectedIndexChanged

    If Not _isLoading  Then
        ' Show message box
    End If

End Sub

另一个选择是删除事件并在需要时将其放回。

答案 1 :(得分:-1)

错误在于此声明中MessageBox.Show(将其更改为msgbox(),然后您的代码将如下所示:

  Private Sub cboNew_DropDown(ByVal sender As Object, ByVal e As EventArgs) Handles cboNew.SelectedIndexChanged

            If cboNew.SelectedItem = "%" Then
                MsgBox(String.Format(" You Selected = " & cboNew.SelectedItem, MessageBoxButtons.OK))
            End If

            If cboNew.SelectedItem = "Value" Then
                MsgBox(String.Format(" You Selected = " & cboNew.SelectedItem, MessageBoxButtons.OK))
            End If

            If cboNew.SelectedItem = "REVOKE" Then
                MsgBox(String.Format(" You Selected = " & cboNew.SelectedItem, MessageBoxButtons.OK))
            End If
 End Sub