Multiple dynamic MenuItems with a single Clic Event Handler

J

Jeronimo Bertran

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
 
N

Norman Yuan

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;
...
...
}
}
 
J

Jeronimo Bertran

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
 
J

Jeffrey Tan[MSFT]

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
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
H

Herfried K. Wagner [MVP]

Norman Yuan said:
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;
...
...
}
}

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

Jeffrey Tan[MSFT]

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
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
J

Jeronimo Bertran

Jeffrey...
yes, I used the text property and it worked fine.

Thanks again.

(e-mail address removed) ("Jeffrey Tan[MSFT]") wrote in
 
J

Jeffrey Tan[MSFT]

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
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 

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