ToolStripSeperator to a ToolStripMenuItem
-
Hi, I go through a collection of toolstripmenuitems and if its the correct item, i set it to be checked. foreach (ToolStripMenuItem sortItemDropDown in sortItem.DropDownItems) { if (sortItemDropDown.Name.Equals(tickSortBy)) { sortItemDropDown.Checked = true; } } The problem is that the sortedItem.DropDownItems has a Seperator in it, thus i get an exception telling me i cant convert a seperator to a menuitem. Is there a work-around ? Regards, Gareth.
-
Hi, I go through a collection of toolstripmenuitems and if its the correct item, i set it to be checked. foreach (ToolStripMenuItem sortItemDropDown in sortItem.DropDownItems) { if (sortItemDropDown.Name.Equals(tickSortBy)) { sortItemDropDown.Checked = true; } } The problem is that the sortedItem.DropDownItems has a Seperator in it, thus i get an exception telling me i cant convert a seperator to a menuitem. Is there a work-around ? Regards, Gareth.
In the foreach loop use the common base type
ToolStripItem
and only cast to aToolStripMenuItem
if the current item is really aToolStripMenuItem
.foreach (ToolStripItem sortItemDropDown in sortItem.DropDownItems)
{
if (sortItemDropDown.Name.Equals(tickSortBy) && (sortItemDropDown is ToolStripMenuItem))
{
((ToolStripMenuItem) sortItemDropDown).Checked = true;
}
}
"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook
-
Hi, I go through a collection of toolstripmenuitems and if its the correct item, i set it to be checked. foreach (ToolStripMenuItem sortItemDropDown in sortItem.DropDownItems) { if (sortItemDropDown.Name.Equals(tickSortBy)) { sortItemDropDown.Checked = true; } } The problem is that the sortedItem.DropDownItems has a Seperator in it, thus i get an exception telling me i cant convert a seperator to a menuitem. Is there a work-around ? Regards, Gareth.
You can check on the type of the toolstripmenuitem. For the separator this is System.Windows.Forms.ToolStripSeparator.
-
In the foreach loop use the common base type
ToolStripItem
and only cast to aToolStripMenuItem
if the current item is really aToolStripMenuItem
.foreach (ToolStripItem sortItemDropDown in sortItem.DropDownItems)
{
if (sortItemDropDown.Name.Equals(tickSortBy) && (sortItemDropDown is ToolStripMenuItem))
{
((ToolStripMenuItem) sortItemDropDown).Checked = true;
}
}
"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook