I try to change the background color of a menu, but the program crashed when the menu item is a separator. I try to check to see if there is a separator before I make the change, but I cannot get the checking type to work
PHP Code:
foreach (ToolStripMenuItem item in items)
{
if (!item is ToolStripSeparator)
{
item.BackColor = color;
if (item.DropDownItems.Count > 0)
{
SetItemsBackColor(color, item.DropDownItems);
}
}
}
It is not clear to me that my suggested change achieves your goals, but give this a try and see if it works for ya ...
Code:
// **************************************************************************
private void SetItemsBackColor(Color color, ToolStripItemCollection items) {
foreach (ToolStripItem item in items) {
if (item is ToolStripMenuItem) {
item.BackColor = color;
// following is commented out because it seems vaguely recursive
// if (((ToolStripMenuItem)item).DropDownItems.Count > 0) {
// SetItemsBackColor(color, ((ToolStripMenuItem)item).DropDownItems);
// }// end if DropDownItems present
}// end if item is ToolStripMenuItem
}// end FOREACH ...
}// end function 'SetItemsBackColor'
// **************************************************************************
Did you copy and paste my code ? just commenting out the code as I did isn't enuf. I changed a type or two.
It is working on my machine.
let's do a line-by-line comparison ...
my code
Code:
// **************************************************************************
private void SetItemsBackColor(Color color, ToolStripItemCollection items) {
foreach (ToolStripItem item in items) {
if (item is ToolStripMenuItem) {
item.BackColor = color;
// following is commented out because it seems vaguely recursive
// if (((ToolStripMenuItem)item).DropDownItems.Count > 0) {
// SetItemsBackColor(color, ((ToolStripMenuItem)item).DropDownItems);
// }// end if DropDownItems present
}// end if item is ToolStripMenuItem
}// end FOREACH ...
}// end function 'SetItemsBackColor'
// **************************************************************************
original code
Code:
foreach (ToolStripMenuItem item in items)
{
if (!item is ToolStripSeparator)
{
item.BackColor = color;
if (item.DropDownItems.Count > 0)
{
SetItemsBackColor(color, item.DropDownItems);
}
}
}
Last edited by ThermoSight; February 10th, 2011 at 03:54 PM.
It is possible to check the for the separator and change that color as well. Wile the bacground color of the menu changes, but it will be nice to identify the separator and change its color
OK, if it still doesn't work after making your code identical to the suggested changes (note the type changes in the first two lines: foreach and if), then we'll drop back six and punt.
But it is working on my machine: no crashes, and the backcolors are set to blue.
re your comment about changing the separator back color, I guess you can do that as well. Pretty colorful display you've got there.
Last edited by ThermoSight; February 10th, 2011 at 04:02 PM.
it works fine; the oly problem is that I wonder if it is possible to change the color of the menu item separator. I coulfn't find a way to change its color.
* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.