Dynamic ContextMenu

G

Guest

Hi,

I have a tricky problem on my hands. Not sure if it is possible or not.
Basically, in my app I dynamically create MenuItems for a ContextMenu. This
part is fine, however, when I click the menu item, I want to capture the text
which is present on that menu item. How can I capture the text that is
present on that menu item? The number of menu items is not fixed. Could I
somehow dynamically create Click events ? And even if i did that, wouldnt I
need to provide the implementation of that click event? Or could I have some
kind of generic click event which can be used for each menu item?

Any pointers or suggestions most welcome.

Cheers,
David
 
G

Guest

David,
You should assign a click event to the dynamically created menu items. You
can assign the same event to all such menu items. In the click event there
is a method parameter called object sender. You can cast the sender object
to a MenuItem and then use that to retrieve your menu item info. I'd use the
following code for the cast to ensure you don't throw an unwanted exception:

MenuItem myItem = sender as MenuItem;

// Sender was a menu item and all is well
if(myItem != null)
{
// Process my stuff here.
}

I hope this helps.

Regards,
R. Davis
Contractor
 
J

Jerod Houghtelling

Hi,

I have a tricky problem on my hands. Not sure if it is possible or not.
Basically, in my app I dynamically create MenuItems for a ContextMenu. This
part is fine, however, when I click the menu item, I want to capture the text
which is present on that menu item. How can I capture the text that is
present on that menu item? The number of menu items is not fixed. Could I
somehow dynamically create Click events ? And even if i did that, wouldnt I
need to provide the implementation of that click event? Or could I have some
kind of generic click event which can be used for each menu item?

Any pointers or suggestions most welcome.

Cheers,
David

You can create a generic click event handler and then check the
senders text. Here's an example in C#.

private void CreateContextMenuItems()
{
MenuItem item1 = new MenuItem();
item1.Text = "Item 1";
item1.Click += new EventHandler( ContextMenuClick );

MenuItem item2 = new MenuItem();
item2.Text = "Item 2";
item2.Click += new EventHandler( ContextMenuClick );
}

private void ContextMenuClick( object sender, EventArgs e )
{
MenuItem clickedMenuItem = sender as MenuItem;

//Figure out which one was clicked.
if( clickedMenuItem.Text == "Item 1" )
{
//do something
}
else if( clickedMenuItem.Text == "Item 2" )
{
//do something
}
}
 
G

Guest

Thanks. That is working perfectly. This is nice as well, MenuItem myItem =
sender as MenuItem; Casting the sender to another object is very useful.

Cheers,
David
 
G

Guest

Thanks. Works nice.

David

Jerod Houghtelling said:
You can create a generic click event handler and then check the
senders text. Here's an example in C#.

private void CreateContextMenuItems()
{
MenuItem item1 = new MenuItem();
item1.Text = "Item 1";
item1.Click += new EventHandler( ContextMenuClick );

MenuItem item2 = new MenuItem();
item2.Text = "Item 2";
item2.Click += new EventHandler( ContextMenuClick );
}

private void ContextMenuClick( object sender, EventArgs e )
{
MenuItem clickedMenuItem = sender as MenuItem;

//Figure out which one was clicked.
if( clickedMenuItem.Text == "Item 1" )
{
//do something
}
else if( clickedMenuItem.Text == "Item 2" )
{
//do something
}
}
 

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