2

How can you add a click event to dynamically created menu item?

I thought I could do something like

Loop through all the items in the Menu1.DropDownItems then create a mousedown even on the item and execute an action based off that.

I'm new to VB and was wondering what logic to use. Will that even work? How will the events be saved through the life of the application?

1 Answer 1

3

You will have to have a method that fits the event's signature. Then, when creating the MenuItem, you can add a handler to the event:

Dim item As New MenuItem(...)
'...
AddHandler item.Click, AddressOf myEventHandler

Sub myEventHandler(sender As Object, e As System.EventArgs)
    'Do something
End Sub

You cannot create an event in a class that you don't have access to. The only option would be to derive from it, but this works only in some cases. In case of the MenuItem, this is not even necessary, because it already provides a Click event. You just have to add a handler for it. The handler is saved in the item's event, which maintains a kind of list of handlers.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.