none
Control crashes when using Windows Classic Theme

    Question

  • Hi,
    I have a control that is using a custom tree view, the problem that I am having is that when using the 'Windows Classic Theme' in XP (i.e. visual styles are turned off) I get the following error:

    Visual Styles-related operation resulted in an error because no visual styles is currently active.
    ************** Exception Text **************
    System.InvalidOperationException: Visual Styles-related operation resulted in an error because no visual style is currently active.
       at System.Windows.Forms.VisualStyles.VisualStyleRenderer.IsCombinationDefined(String className, Int32 part)
       at System.Windows.Forms.VisualStyles.VisualStyleRenderer..ctor(String className, Int32 part, Int32 state)
       at System.Windows.Forms.VisualStyles.VisualStyleRenderer..ctor(VisualStyleElement element)
       at AdvancedDataGridView.TreeGridView..ctor()
       at ServicesTree.InitializeComponent()
       at ServicesTree..ctor(Int32 alertIdIn)

    I found a peice of code that may have been using visual styles so I put the following if statement around it:
    if (VisualStyleInformation.IsSupportedByOS && VisualStyleInformation.IsEnabledByUser)
                    treeGridView1.EnableHeadersVisualStyles = true;


    However this did not solve the problem, I have found one more peice of code that uses visual styles:
    internal VisualStyleRenderer rOpen = new VisualStyleRenderer(VisualStyleElement.TreeView.Glyph.Opened);
    ...but that's a definition so I can't wrap it in an if statement like above.

    Does anybody know how I can get round this? Is it even going to be possible for me to use this treeview with visual styles disabled?

    Thanks in advance for any help,
    Tom
    Monday, December 11, 2006 9:49 AM

Answers

  • Hi there,

    On the author's website:
    http://blogs.msdn.com/markrideout/archive/2006/01/08/510700.aspx

    one of the comments contains the fix you need (someone else realized it requires styles, and wrote a check in case they're not available).  In case that blog ever disappears, here is the relevant code:


    in "TreeGridView.cs" delete the followin' two lines:

    ------8<------
    internal VisualStyleRenderer rOpen = new VisualStyleRenderer(VisualStyleElement.TreeView.Glyph.Opened);
    internal VisualStyleRenderer rClosed = new VisualStyleRenderer(VisualStyleElement.TreeView.Glyph.Closed);
    ------8<------

    in "TreeGridCell.cs" find the line "if (node.HasChildren || node._grid.VirtualNodes)" and change it so that it looks like that:

    ------8<------
               if (node.HasChildren || node._grid.VirtualNodes)
               {
                   // Ensure that visual styles are supported.
                   if (Application.RenderWithVisualStyles)
                   {
                       VisualStyleRenderer rOpen = new VisualStyleRenderer(VisualStyleElement.TreeView.Glyph.Opened);
                       VisualStyleRenderer rClosed = new VisualStyleRenderer(VisualStyleElement.TreeView.Glyph.Closed);

                       // Paint node glyphs
                       if (node.IsExpanded)
                           //node._grid.rOpen.DrawBackground(graphics, new Rectangle(glyphRect.X, glyphRect.Y + (glyphRect.Height / 2) - 4, 10, 10));
                           rOpen.DrawBackground(graphics, new Rectangle(glyphRect.X, glyphRect.Y + (glyphRect.Height / 2) - 4, 10, 10));
                       else
                           //node._grid.rClosed.DrawBackground(graphics, new Rectangle(glyphRect.X, glyphRect.Y + (glyphRect.Height / 2) - 4, 10, 10));
                           rClosed.DrawBackground(graphics, new Rectangle(glyphRect.X, glyphRect.Y + (glyphRect.Height / 2) - 4, 10, 10));
                   }
                   else
                   {
                       int h = 8;
                       int w = 8;
                       int x = glyphRect.X;
                       int y = glyphRect.Y + (glyphRect.Height / 2) - 4;
                       //MessageBox.Show("x = " + x.ToString() + ", y= " + y.ToString());

                       graphics.DrawRectangle(new Pen(SystemBrushes.ControlDark), x, y, w, h);
                       graphics.FillRectangle(new SolidBrush(Color.White), x + 1, y + 1, w - 1, h - 1);
                       graphics.DrawLine(new Pen(new SolidBrush(Color.Black)), x + 2, y + 4, x + w - 2, y + 4);

                       if (!node.IsExpanded)
                           graphics.DrawLine(new Pen(new SolidBrush(Color.Black)), x + 4, y + 2, x + 4, y + h - 2);
                   }
               }
    ------8<------
    Wednesday, December 13, 2006 4:07 AM

All replies

  • The control will fail if visual styles is not enabled.  There is no work around.  The control was improperly created anyway.  It should not make any assumptions about the renderer being used.  Instead it should verify that visual styles are supported and used before trying to create them.  You'll need to either modify the code or use a different control.  Since the code sits inside the constructor of the class you don't have much of an option to avoid it.

    BTW please post WinForms related questions to the WinForms forum.  Please reserve the C# forum for questions specifically related to the C# language.  I'm moving the thread to the WinForms forum.  Thanks.

    Michael Taylor - 12/11/06

    Monday, December 11, 2006 2:20 PM
  • That control was written with theming support required.  Visit the author's blog to ask for alternatives.
    Monday, December 11, 2006 7:41 PM
    Moderator
  • That's what I was expecting the answer would be Thanks guys.
    Tuesday, December 12, 2006 4:39 PM
  • Hi there,

    On the author's website:
    http://blogs.msdn.com/markrideout/archive/2006/01/08/510700.aspx

    one of the comments contains the fix you need (someone else realized it requires styles, and wrote a check in case they're not available).  In case that blog ever disappears, here is the relevant code:


    in "TreeGridView.cs" delete the followin' two lines:

    ------8<------
    internal VisualStyleRenderer rOpen = new VisualStyleRenderer(VisualStyleElement.TreeView.Glyph.Opened);
    internal VisualStyleRenderer rClosed = new VisualStyleRenderer(VisualStyleElement.TreeView.Glyph.Closed);
    ------8<------

    in "TreeGridCell.cs" find the line "if (node.HasChildren || node._grid.VirtualNodes)" and change it so that it looks like that:

    ------8<------
               if (node.HasChildren || node._grid.VirtualNodes)
               {
                   // Ensure that visual styles are supported.
                   if (Application.RenderWithVisualStyles)
                   {
                       VisualStyleRenderer rOpen = new VisualStyleRenderer(VisualStyleElement.TreeView.Glyph.Opened);
                       VisualStyleRenderer rClosed = new VisualStyleRenderer(VisualStyleElement.TreeView.Glyph.Closed);

                       // Paint node glyphs
                       if (node.IsExpanded)
                           //node._grid.rOpen.DrawBackground(graphics, new Rectangle(glyphRect.X, glyphRect.Y + (glyphRect.Height / 2) - 4, 10, 10));
                           rOpen.DrawBackground(graphics, new Rectangle(glyphRect.X, glyphRect.Y + (glyphRect.Height / 2) - 4, 10, 10));
                       else
                           //node._grid.rClosed.DrawBackground(graphics, new Rectangle(glyphRect.X, glyphRect.Y + (glyphRect.Height / 2) - 4, 10, 10));
                           rClosed.DrawBackground(graphics, new Rectangle(glyphRect.X, glyphRect.Y + (glyphRect.Height / 2) - 4, 10, 10));
                   }
                   else
                   {
                       int h = 8;
                       int w = 8;
                       int x = glyphRect.X;
                       int y = glyphRect.Y + (glyphRect.Height / 2) - 4;
                       //MessageBox.Show("x = " + x.ToString() + ", y= " + y.ToString());

                       graphics.DrawRectangle(new Pen(SystemBrushes.ControlDark), x, y, w, h);
                       graphics.FillRectangle(new SolidBrush(Color.White), x + 1, y + 1, w - 1, h - 1);
                       graphics.DrawLine(new Pen(new SolidBrush(Color.Black)), x + 2, y + 4, x + w - 2, y + 4);

                       if (!node.IsExpanded)
                           graphics.DrawLine(new Pen(new SolidBrush(Color.Black)), x + 4, y + 2, x + 4, y + h - 2);
                   }
               }
    ------8<------
    Wednesday, December 13, 2006 4:07 AM