Event Handling With Dynamic ToolStripMenu

A

AmericanGotham

The following method adds items to a toolstripmenu named colums

public void AddColumnMenu()
{
foreach (ColumnHeader column in messagesListView.Columns)
{

ToolStripMenuItem item =
(ToolStripMenuItem)columnsToolStripMenuItem.DropDownItems.Add(column.Text);
item.Checked = true;
item.CheckOnClick = true;

}
}

How do you add event handling for each menu item added? What I am
going to end up doing is allow the user to expand or remove a column
by clicking that column name in the columns menu.
 
L

Laura T.

What's wrong wiht good old ToolStripMenuItem.Click+=new EventHandler(<your
handler>); ?

Something like:

ToolStripMenuItem item =
(ToolStripMenuItem)columnsToolStripMenuItem.DropDownItems.Add(column.Text);
item.Checked = true;
item.CheckOnClick = true;
item.Click+=new EventHandler(HandleDynamicMenuClick);

}

void HandleDynamicMenuClick(object sender, EventArgs e)
{
ToolStripItem item=(ToolStripItem)sender;
MessageBox.Show("Selected menu " + item.Text);
}
 
A

AmericanGotham

Yep.. thats what I ended up doing. Im kind of a C# newb. Sorry for
the dumb question lol
 

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