HOWTO: Consuming ToolStripMenuItem.DropDownItemClicked in VB.NET

300 Views Asked by At

I have a toolStripMenuItem which is a menu containing other dropdown items. When DropDownItemClicked event is raised by selection one of the dropdown items I would like to consume the event if some condition is satisfied, like below:

Private Sub tsmi_DropDownItemClicked( _
        ByVal sender As Object, _
        ByVal e As System.Windows.Forms.ToolStripItemClickedEventArgs) _
        Handles tsmi.DropDownItemClicked


        ...

        If some_condition_is_satisfied then
           e.Cancel = True <------ Cancel is not available in this event!
        End If

        ...
End Sub

The problem is that DropDownItemClicked does not provided the posibility to perform:

e.Cancel = True

So how could I consume this event?

1

There are 1 best solutions below

0
On

If you just want to terminate the sub, then

If some_condition_is_satisfied then
    exit sub
End If

would do the trick - even in event handlers. However, if you want the program to be notified if the sub is cancelled then you will need to declare a boolean variable that has a class level scope and set he variable to false at the beginning of the sub and in the If..Then condition, set it to true.