Question

I am building a custom control and what i would like to do is have an event lets call this event OnMenuShow. Now what i would like to be able to do is handle this event inside my control to show one menu but allow the user implementing my custom control to handle the event in inside the parent form to show a different menu if they wish. so the users code would look something like this.

    Public Sub Control_OnMenuShow(sender as Object, e as CustomEventArgs) handles Control.OnMenyShow
      'DO some work
       e.handled = true
    end Sub

I'm just not sure on how to prevent the event from firing twice once for the code inside the control the other in the event. if someone could point me in the right direction that would be very helpful

-Nathan

Was it helpful?

Solution 2

I found that Hans' answer reponse fit my question the best, and had he left it in an answer I would have marked his as answered. I will leave how I ended up coding this for future refrence.

Public Event MenuShow As EventHandler(Of  MenuShowEventArgs)

Public Overridable Sub OnMenuShowEvent()
    Dim args As New MenuShowEventArgs(False, "Control")
    RaiseEvent MenuShow(Me, args)
    if args.handled then return
    'DO WORK
End Sub

OTHER TIPS

In your control:

Public Event MenuShow As EventHandler



Public Overridable Sub OnMenuShow()
  RaiseEvent MenuShow(New EventArgs)
End Sub

Now the consumer may override OnMenuShow which would bypass your raiseevent statement without you needing to "check" anything.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top