Search code examples
vb.netdropdownlistfor

Pop up message on selectindexchanged in vb


I was wondering how can I display a pop up warning message every time the selected item in dropdown list has changed. I've managed to get it working , but the message will unfortunately also pop up when loading the form as in some cases the default value has already been set and is not empty. So far i have this:

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

Solution

  • The mistake is in this statement MessageBox.Show( change this as msgbox() then your code will be like the following:

      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