views:

2323

answers:

2

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.

A: 

You are reference checking when you do

filterMenu.DropDownItems[s[0]].

You don't match the value of s[0] with the items in DropDownItems but you match their references, and those don't match.

You either need to loop through all items and do a manual check if their value is the same, or you find a way to insert your own comparator into that process.

Tigraine
Hmm. I can't really test this due to the fact that I don't have a Dropdowncontrol in my VS2008.
Tigraine
A: 

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..
Hans Passant