Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm General / April 2005

Tip: Looking for answers? Try searching our database.

Multiple dynamic MenuItems with a single Clic Event Handler

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Jeronimo Bertran - 12 Apr 2005 03:45 GMT
Hello,

I need to create a menu dynamically where the number of items is variable
but I don't know how to add a single click event handler with a way to
distinguish which item caused the click event:

foreach (MyType obj in ObjectTypes)
{
    MenuItem item = new MenuItem(obj.title);

       // Ideally pass on obj.id to the event handler.
    item.Click += new System.EventHandler(this.typedMenuItem_Click);

    menuItemFileNew.MenuItems.Add(item);
}

Thanks,

Jeronimo
Norman Yuan - 12 Apr 2005 04:27 GMT
How are about this:

private void typedMenuItem_Click(object sender, EventArgs e)
{
   MenuItem mnu=(MenuItem)sender;
   switch(mnu.Name)
   {
       case "MenuName1":
           //Do one thing
           break;
       case "MuniName 2":
           //Do other thing
           break;
       ...
       ...
   }
}
> Hello,
>
[quoted text clipped - 15 lines]
>
> Jeronimo
Jeronimo Bertran - 12 Apr 2005 05:59 GMT
Thanks Norman,

The menu items are actually dynamic.. but I can make your suggestion work
by looking for the name on the collection...

thanks.

Jeronimo
"Jeffrey Tan[MSFT]" - 13 Apr 2005 02:06 GMT
Hi Jeronimo,

Thanks for your feedback!!

I am glad that you got what you want, but just as "Herfried K. Wagner
[MVP]" and I pointed, the MenuItem does not have a "Name" property, and the
compiler will generate error on building Name property. Actually, Name
property is added by the VS.net designer for only design-time usage. So I
suspect you actually used MenuItem.Text property, yes?

Anyway, if you need further help, please feel free to tell us. I will work
with you. Thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Signature

Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Herfried K. Wagner [MVP] - 12 Apr 2005 15:35 GMT
"Norman Yuan" <NoOne@NoExist.No> schrieb:
> private void typedMenuItem_Click(object sender, EventArgs e)
> {
[quoted text clipped - 11 lines]
>    }
> }

'MenuItem' doesn't have a 'Name' property.

Signature

M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://classicvb.org/petition/>

Herfried K. Wagner [MVP] - 13 Apr 2005 10:14 GMT
Addendum:

> 'MenuItem' doesn't have a 'Name' property.

You may want to use a menuitem extender to extend the 'MenuItem' class with
properties:

MenuItem Extender - add images and font support to your menu
<URL:http://www.codeproject.com/cs/menu/MenuExtender.asp>

Signature

M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://classicvb.org/petition/>

"Jeffrey Tan[MSFT]" - 12 Apr 2005 06:23 GMT
Hi Jeronimo,

Thanks for your post!!

I think Norman have provided you one option way: use MenuItem.Text property
to distinguish the items(In design-time, there is no Name property). This
makes sense, because normally, we will not use 2 menuitems with the same
text.

Also, if all the menuitems are the same parent's children, we may use Index
property to determine their position. But I think the Text property options
is better in your situation.
=====================================
Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Signature

Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Jeronimo Bertran - 15 Apr 2005 01:02 GMT
Jeffrey...
yes, I used the text property and it worked fine.

Thanks again.

> Hi Jeronimo,
>
> Thanks for your post!!
"Jeffrey Tan[MSFT]" - 15 Apr 2005 02:27 GMT
Hi Jeronimo,

Oh, thanks for your feedback and confirmation :-). I am glad your problem
is resolved. If you need further help, please feel free to post. Thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Signature

Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.