Anders
Anders

Reputation: 1

C# - Checking if ContextMenu contains an item

I have a ContextMenu with some items, at runtime I add new items to that ContextMenu and I want to know how I can check if the ContextMenu contains the item, if the same item is already in the ContextMenu then dont add the item again. Only add items that are not in the ContextMenu.

How can I do that?

Upvotes: 0

Views: 1932

Answers (1)

MBen
MBen

Reputation: 3996

this works perfectly :

    ContextMenu menu = new ContextMenu();
    MenuItem item = new MenuItem();

    menu.MenuItems.Add(0, item);

    if (menu.MenuItems.Contains(item))
        Console.WriteLine("The item exists");

Upvotes: 1

Related Questions