Dropdown items on ToolstripmenuItem and checking if their items are checked

I am building a small Winform in which I can view the types of food in my kitchen.

All my stock can be displayed on the datagrid view.

I now have a filtermenu that contains a dropdown list of items that can be checked and unchecked.

Depending on which items in this list are checked, it is displayed in the datagridview. Only the selected items are displayed.

At least the way I want it. The menu currently has 5 items: meat, fish, fruits, vegetables and more.

I am using the abstract Food class and all other classes derive from it. In the end, I make a string representation of each piece of food that looks something like this.

FoodType * FoodName * AmountInStock * ...

So, star * as separator.

Then i do this

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

      

From FoodList is ArrayList. Then I debug this with VisualStudio 2008

The o object always contains null.

However, s [0] always contains the name of the food type. What I want is to find out which subject is being tested on this menulist. If the checkbox is checked, the datagridview should display it. If not, don't show it.

I fill it in the constructor like 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 go here.

0


source to share


3 answers


You can use the IndexOfKey property to find ToolStripMenuItem. This requires setting the Name property when adding it:



ToolStripMenuItem it = (ToolStripMenuItem)item.DropDownItems.Add(element);
it.Name = element;
// etc..

      

0


source


You check links when you do

filterMenu.DropDownItems [since [0]].



You do not map the value of s [0] to items in DropDownItems, but you do match their references and they do not match.

You need to either iterate over all the elements or do a manual check if their value matches, or you find a way to insert your own comparator into this process.

0


source


When you populated your MenuStrip from your constructor, you added items without giving them a name. Then you tried to access the elements by name:

Object o = filterMenu.DropDownItems[s[0]]; //null  Name??

      

If you add an element and give it a name, you can access the elements:

ToolStripMenuItem it = (ToolStripMenuItem)item.DropDownItems.Add(element);
it.Name = element;  // This was missing

      

Based on the OP's question, I want to find out if this menulist's item is checked. This code will show you how to find a menu item that has been checked:

filterMenu.DropDownItems.Cast<ToolStripMenuItem>().Where(tsi=>tsi.Checked).First().Text

      

0


source







All Articles