Change Background of Menu
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);
}
}
}
any hint on getting this to work
Re: Change Background of Menu
What line does it crash on? Set a break point, step through the code and find out.
Re: Change Background of Menu
it crashed somewhere here
foreach (ToolStripMenuItem item in items)
after interacting through the menu and encounter a menu separator
Re: Change Background of Menu
And what indications did the debugger give ? "it crashed somewhere here" really isn't a lot to work from.
perhaps you could post the code ?
1 Attachment(s)
Re: Change Background of Menu
comment this line out and remove the separator you can see it works fine
if (!item is ToolStripSeparator)
Re: Change Background of Menu
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'
// **************************************************************************
Re: Change Background of Menu
Still crashed on the same spot event when commented out your suggestion
Re: Change Background of Menu
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);
}
}
}
Re: Change Background of Menu
sorry, I did not copy it. I commetted the line out.
Re: Change Background of Menu
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
Re: Change Background of Menu
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.
Re: Change Background of Menu
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.