Creating MenuItems programaticialy

  • Thread starter Ray Cassick \(Home\)
  • Start date
R

Ray Cassick \(Home\)

I am in a bit of a bind. I can't believe I cannot figure this out but I have
been at it all day and it is just not coming to me.

I am building menus on a form based upon an XML file. I have created a class
to do this and that part is all working just fine. My problem is the event
handler where I want all the click events to go. Not really building the
event hander, just determining the individual menuItem that triggered the
handler is my problem.

My event handler looks like this:

Private Sub MenuClick(ByVal sender As Object, ByVal e As System.EventArgs)

Dim menuItem As MenuItem

menuItem = sender

Debug.WriteLine("--" & menuItem.Text)

End Sub


What I want to be able to do it look at something on the sender object (like
a name or tag property) and determine what exact MenuItem triggered the
event (I have all my menuitems going to the same handler).

I thought that when I was creating my individual MenuItems I would be able
to assign each of them an individual name like:

mnuFile
mnuFileNew
mnuFileNewMessage
.... etc ...

and then be able to get then in code as something like sender.name or
something, but I can't seem to be able to do that.

All that I seem to be able to do it get the Text of the sender and this just
gets me the menu text, not really helpful in the long run.

Besides rolling my own MenuItem class just to add some type of property like
Tag or Name, is there any way that I can do this? I know that I can use
sender.GetType().Name but all this does is return the string 'MenuItem' and
that is no good to me.
 
K

Ken Tucker [MVP]

Hi,

The hashtable allows you store items based on a key. The key is of
the object type. Why dont you store what you want to be a tag in a
hashtable with the menuitem as the key.

Ken
 
R

Ray Cassick \(Home\)

Interesting... Using the actual object as the key...

I will have to give it a try.. Thanks for the suggestion.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top