CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12
  1. #1
    Join Date
    Jun 2002
    Posts
    936

    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(coloritem.DropDownItems);
                        }
                    }
                } 
    any hint on getting this to work

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Change Background of Menu

    What line does it crash on? Set a break point, step through the code and find out.

  3. #3
    Join Date
    Jun 2002
    Posts
    936

    Re: Change Background of Menu

    it crashed somewhere here

    foreach (ToolStripMenuItem item in items)

    after interacting through the menu and encounter a menu separator

  4. #4
    Join Date
    Oct 2005
    Location
    Seattle, WA U.S.A.
    Posts
    353

    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 ?

  5. #5
    Join Date
    Jun 2002
    Posts
    936

    Re: Change Background of Menu

    comment this line out and remove the separator you can see it works fine

    if (!item is ToolStripSeparator)
    Attached Files Attached Files

  6. #6
    Join Date
    Oct 2005
    Location
    Seattle, WA U.S.A.
    Posts
    353

    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'
            // **************************************************************************

  7. #7
    Join Date
    Jun 2002
    Posts
    936

    Re: Change Background of Menu

    Still crashed on the same spot event when commented out your suggestion

  8. #8
    Join Date
    Oct 2005
    Location
    Seattle, WA U.S.A.
    Posts
    353

    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); 
                        } 
                    } 
                }
    Last edited by ThermoSight; February 10th, 2011 at 03:54 PM.

  9. #9
    Join Date
    Jun 2002
    Posts
    936

    Re: Change Background of Menu

    sorry, I did not copy it. I commetted the line out.

  10. #10
    Join Date
    Jun 2002
    Posts
    936

    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

  11. #11
    Join Date
    Oct 2005
    Location
    Seattle, WA U.S.A.
    Posts
    353

    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.
    Last edited by ThermoSight; February 10th, 2011 at 04:02 PM.

  12. #12
    Join Date
    Jun 2002
    Posts
    936

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured