How to loop through dropdown items in a toolstrip

3.1k views Asked by At

I have a dropdown in my toolstrip (toolbar) i have attached a click event to each item in drop down as the dropdown has been populated dynamically, now i can casted the selected item in the dropdown and set its state to checked, to have a tick next to it, i wish to have a loop in another method to check which item has been checked. How do i loop through the items in the dropdown checking which item is checked?

  foreach (DataSet1.xspLibraryByNameRow libName in data.xspLibraryByName)
        {
            var name = new LibraryItems(libName);
            if (libName.xlib_Code != "NULL")
            {
                catDrpDwn.DropDown.Items.Add(name);
                catDrpDwn.DropDown.Tag = name;
                name.Click += new EventHandler(name_Click);
            }
        }

    }

    void mapArea_VE_MapReady(object sender, EventArgs e)
    {
        loadPoints();
    }

    void name_Click(object sender, EventArgs e)
    {
        var selected = (LibraryItems)sender;
        selected.Checked = true;

        loadPoints();
    }
2

There are 2 answers

0
floormind On BEST ANSWER
        foreach (var items in catDrpDwn.DropDown.Items)
        {
            var it = (LibraryItems)items;
            if (it.Checked == true)
            {

            }
        }
0
Manish Parakhiya On

Try this

var items=catDrpDwn.DropDown.Items.Cast<LibraryItems>().Where(d=>d.Checked).ToList();

here you will get all checked items and you can loop into it.