Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically adding ToolStripMenuItems to a MenuStrip (C#/ Winforms)

I have my solution implemented (basic solution) and I'm happy.

Problem is when I add new items to a ToolStripItemCollection using the 'Add' method, I get a few overloads ... the meaningful one being a string parameter, an image parameter and an EventHandler parameter.

Because my drop down list is going to be serving as a dynamic history at RunTime, means it going to be empty at compile time. This means I can't add an event handler through the standard route of using the designer surface (On click). I am forced to use the overload described above.

I image is of no use to me but adding the event handler dynamically is what I am interested in and need help with.

URL: http://msdn.microsoft.com/en-us/library/bxdt0s8t.aspx

There is no other overload to help me, so I have to use an Image ... anyone got any ideas to get around this and show me how to fully satisfy this overloaded version of the add method.

TIA.

UPDATE: I re did this again in a current project but using more slicker code but the principle is the same, add event handlers dynamically at run time. I will update this with some sample code when I get home.

like image 341
IbrarMumtaz Avatar asked Nov 18 '09 17:11

IbrarMumtaz


People also ask

How to add MenuStrip Items Dynamically in c#?

To add a MenuStrip Dynamically at run-time we create an instance of the "MenuStrip" Class. MenuStrip MnuStrip = new MenuStrip (); To add the MenuStrip control to the Form, we use the "Add" property: this.

How to add SubMenu in MenuStrip c#?

To add a SubMenu to the MenuStrip, we use ToolStripMenuItem by creating an instance of it. it to the ToolStripMenuItem using the DropDownItems collection and adding it to the ToolStripMenuItem. adding it to the existing ToolStripMenuItem instance.

What is MenuStrip in C#?

C# MenuStrip: Windows FormsUse the MenuStrip control in Windows Forms to add a menu bar to your program window. MenuStrip adds a menu bar to your Windows Forms program. With this control, we add a menu area and then add the default menus or create custom menus directly in Visual Studio.


2 Answers

The way I do it is to create an array of ToolStripMenuItems and populate that array with the items I'm adding. I create one method to handle the click events and have it check something unique about each item I create at run-time. You might try using the Name or Tag properties of each ToolStripMenuItem. Then use AddRange on the spot in the menu you're adding to. So your code might look something like this:

private void BuildMenuItems()
{
    ToolStripMenuItem[] items = new ToolStripMenuItem[2]; // You would obviously calculate this value at runtime
    for (int i = 0; i < items.Length; i++)
    {
        items[i] = new ToolStripMenuItem();
        items[i].Name = "dynamicItem" + i.ToString();
        items[i].Tag = "specialDataHere";
        items[i].Text = "Visible Menu Text Here";    
        items[i].Click += new EventHandler(MenuItemClickHandler);
    }

    myMenu.DropDownItems.AddRange(items);
}

private void MenuItemClickHandler(object sender, EventArgs e)
{
    ToolStripMenuItem clickedItem = (ToolStripMenuItem)sender;
    // Take some action based on the data in clickedItem
}
like image 145
jasonh Avatar answered Nov 15 '22 13:11

jasonh


What is wrong with:

ToolStripItem item = toolStripItems.Add("MyItem");
item.Click += new EventHandler(....);

Am I missing something?

like image 20
Philip Wallace Avatar answered Nov 15 '22 11:11

Philip Wallace