PC Review


Reply
Thread Tools Rate Thread

Creating MenuItems programaticialy

 
 
Ray Cassick \(Home\)
Guest
Posts: n/a
 
      5th Apr 2004
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.








 
Reply With Quote
 
 
 
 
Ken Tucker [MVP]
Guest
Posts: n/a
 
      5th Apr 2004
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
--------------
"Ray Cassick (Home)" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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.
>
>
>
>
>
>
>
>



 
Reply With Quote
 
Ray Cassick \(Home\)
Guest
Posts: n/a
 
      5th Apr 2004
Interesting... Using the actual object as the key...

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

"Ken Tucker [MVP]" <(E-Mail Removed)> wrote in message
news:#(E-Mail Removed)...
> 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
> --------------
> "Ray Cassick (Home)" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > 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.
> >
> >
> >
> >
> >
> >
> >
> >

>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to remove ALT tag from menuitems Exch Microsoft ASP .NET 1 23rd Jan 2007 09:59 PM
Add menuitems to the VBE R Avery Microsoft Excel Programming 0 10th Jul 2004 12:11 AM
RE: Images in Menuitems Serg Kuryata [MS] Microsoft Dot NET Compact Framework 0 24th May 2004 06:49 PM
How do I get the Forms Designer MainMenu object to generate code which uses my MenuItems instead of the base MenuItems =?Utf-8?B?TmlsZXNoIFJhZGU=?= Microsoft Dot NET Framework Forms 5 8th Feb 2004 01:06 PM
UnMerge MenuItems? Gene McCrory Microsoft Dot NET Framework Forms 1 30th Jun 2003 12:54 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 12:19 AM.