views:

2192

answers:

2

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.

+2  A: 

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
}
jasonh
thanks for the replying ... this looks promsing alhough I would like to try out the Add methods overload first that seems like the one for the job if someone on here can show me how .... = ) I remain optimistic ....
IbrarMumtaz
The `Add` method works the same way. Don't create an array and then make the for loop work on a single item: `ToolStripMenuItem item = new ToolStripMenuItem();` and then at the end of the for loop call: `myMenu.Items.Add(item);`
jasonh
Ty I took some clues from your reply and got it working beautifully.Right now the last thing I need to do for my app before I move onto the next big chapter is come up with a method that reshuffles my history menu, to allow a new item to be added and the rest are moved up. This should be simple enuff as I have tested out it out and everything seems brill so far.
IbrarMumtaz
A: 

What is wrong with:

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

Am I missing something?

Philip Wallace
Dunno mate, am relatively inexperienced but thanks to JasonH I was able to use the following. Nothing wrong with that really, I was more keen on using one of the overloads as pose to solutions mentioned above......................................................................... historyToolStripMenuItem.DropDownItems.Add(treeDrives.SelectedNode.FullPath, null, new EventHandler(MenuItemClickHandler)); This is exactly what I was after ... and works beautifully
IbrarMumtaz
As you can see I am using the FullPath property to give the item a meaningful name as it relates to the file that has been launched, null because am not using any images and the event handler at the end. This is the overload that I was struggling with ... seems simple now = ). Incase you are wondering this is going to append recently executed files to the history menu and from the menu re-launch them as a shortcut ^^.
IbrarMumtaz