Ad

Dropdown Items On A ToolstripmenuItem And Seeing If Their Items Are Checked

- 1 answer

I'm building a small Winform in which I can view types of food in my kitchen.

My entire stock can be displayed by a datagrid view.

Now, I have a filtermenu which contains a dropdownlist of items that can be checked and unchecked.

Based on which items in that list are checked, the display in the datagridview is changed. Only items which are selected are displayed.

At least, that's how I want it to be. The menu currently has 5 items: Meat, Fish, Fruit, Vegetable and Other.

I'm using a abstract class Food and all the other classes are derived from it. Eventually I make a string representation of each piece of food which looks a bit like this

FoodType*FoodName*AmountInStock*...

So a star * as seperator.

Then I do this

foreach(Food f in this.kitchen.FoodList)
{
    string[] s = f.ToString().Split('*');
    Object o = filterMenu.DropDownItems[s[0]];
}

With FoodList being an ArrayList. Then I debug that with VisualStudio 2008

The Object o always contains null.

Yet s[0] always contains the name of the food type. What I want is to be able to find out wheter an item on that menulist is checked. If checked, the datagridview must display it. If not, don't display it.

I fill it up in the constructor with this:

public static void Fill(ToolStripMenuItem item, Type food)
{
    foreach (string element in Enum.GetNames(food))
    {
        if (element != "nothing")
        {
            ToolStripMenuItem it = (ToolStripMenuItem)item.DropDownItems.Add(element);
            it.Checked = true;
            it.CheckOnClick = true;
        }
    }
}

I've tried the object browser but I can't find anything that helps, so I turn here.

Ad

Answer

You could use the IndexOfKey property to find the ToolStripMenuItem back. That requires setting the Name property when you add them:

ToolStripMenuItem it = (ToolStripMenuItem)item.DropDownItems.Add(element);
it.Name = element;
// etc..
Ad
source: stackoverflow.com
Ad