Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7548129
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T09:37:27+00:00 2026-05-30T09:37:27+00:00

I am overriding a TreeView so that I can highlight the nodes using better

  • 0

I am overriding a TreeView so that I can highlight the nodes using better colors. As part of the applications options I want to enable the user to change the TreeView‘s font and the font color when both selected and deselected. The code is below:

class MyTreeView : TreeView
{
    // Create a Font object for the node tags and HotTracking.
    private Font hotFont;
    private Font tagFont = new Font("Helvetica", Convert.ToSingle(8.0), FontStyle.Bold);

    #region Accessors.
    public Font hotTrackFont
    {
        get { return this.hotFont; }
        set { this.hotFont = value; }
    }

    //public string unFocusedColor
    //{
    //   get { return this.strDeselectedColor; }
    //   set { this.strDeselectedColor = value; }
    //}

    //public string focusedColor
    //{
    //   get { return this.strSelectedColor; }
    //   set { this.strSelectedColor = value; }
    //}
    #endregion

    public MyTreeView()
    {
        this.HotTracking = true;
        this.DrawMode = TreeViewDrawMode.OwnerDrawText;
        hotFont = new Font(this.Font.FontFamily, this.Font.Size, FontStyle.Underline);
    }

    // Override the drawMode of TreeView.
    protected override void OnDrawNode(DrawTreeNodeEventArgs e)
    {
        TreeNodeStates treeState = e.State;
        Font treeFont = e.Node.NodeFont ?? e.Node.TreeView.Font;

        // Colors.
        Color foreColor = e.Node.ForeColor;

        // Like with the hotFont I want to be able to change these dynamically...
        string strDeselectedColor = @"#6B6E77", strSelectedColor = @"#94C7FC";
        Color selectedColor = System.Drawing.ColorTranslator.FromHtml(strSelectedColor);
        Color deselectedColor = System.Drawing.ColorTranslator.FromHtml(strDeselectedColor);

        // New brush.
        SolidBrush selectedTreeBrush = new SolidBrush(selectedColor);
        SolidBrush deselectedTreeBrush = new SolidBrush(deselectedColor);

        // Set default font color.
        if (foreColor == Color.Empty)
            foreColor = e.Node.TreeView.ForeColor;

        // Draw bounding box and fill.
        if (e.Node == e.Node.TreeView.SelectedNode)
        {
            // Use appropriate brush depending on if the tree has focus.
            if (this.Focused)
            {
                foreColor = SystemColors.HighlightText;
                e.Graphics.FillRectangle(selectedTreeBrush, e.Bounds);
                ControlPaint.DrawFocusRectangle(e.Graphics, e.Bounds, foreColor, SystemColors.Highlight);
                TextRenderer.DrawText(e.Graphics, e.Node.Text, treeFont, e.Bounds,
                                             foreColor, TextFormatFlags.GlyphOverhangPadding);
            }
            else
            {
                foreColor = SystemColors.HighlightText;
                e.Graphics.FillRectangle(deselectedTreeBrush, e.Bounds);
                ControlPaint.DrawFocusRectangle(e.Graphics, e.Bounds, foreColor, SystemColors.Highlight);
                TextRenderer.DrawText(e.Graphics, e.Node.Text, treeFont, e.Bounds,
                                             foreColor, TextFormatFlags.GlyphOverhangPadding);
            }
        }
        else
        {
            if ((e.State & TreeNodeStates.Hot) == TreeNodeStates.Hot)
            {
                e.Graphics.FillRectangle(SystemBrushes.Window, e.Bounds);
                TextRenderer.DrawText(e.Graphics, e.Node.Text, hotFont, e.Bounds,
                                             System.Drawing.Color.Black, TextFormatFlags.GlyphOverhangPadding);
            }
            else
            {
                e.Graphics.FillRectangle(SystemBrushes.Window, e.Bounds);
                TextRenderer.DrawText(e.Graphics, e.Node.Text, treeFont, e.Bounds,
                                             foreColor, TextFormatFlags.GlyphOverhangPadding);
            }
        }
    }
}

As you can see I am currently changing the Font hotFont using an accessor to the class which seems to work. However, when I try to edit the colors of the focused/unfocused node, VS2010 crashes! What is it that is causing this behaviour exactly and how can I achieve what I want?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T09:37:28+00:00Added an answer on May 30, 2026 at 9:37 am

    The posted code does not recreate the error when I enabled those commented out properties and added the variables to the control’s scope.

    A couple of notes though:

    Instead of string properties, I would use an actual color:

    private Color _UnfocusedColor = ColorTranslator.FromHtml(@"#94C7FC");
    private Color _FocusedColor = ColorTranslator.FromHtml(@"#6B6E77");
    
    public Color UnfocusedColor
    {
       get { return _UnfocusedColor; }
       set { _UnfocusedColor = value; }
    }
    
    public Color FocusedColor
    {
      get { return _FocusedColor; }
      set { _FocusedColor = value; }
    }
    

    Also, make sure to dispose of your drawing objects, such as the SolidBrush objects, or wrap them up in a Using(...){} block.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Overriding a method can be prevented by using the keyword final , likewise how
I noticed that when overriding virtual methods in C# using Visual Studio, the IDE
I am overriding Devise's failure response so that I can set a 401 status
I know that overriding the back button functionality is not considered a good user
When overriding the equals() function of java.lang.Object, the javadocs suggest that, it is generally
I'm overriding WndProc, so I want to write code like if (m.Msg == WM_COMMAND)
I am overriding the clean() method on a Django form. I want to have
I'm overriding the clean method for my variables and want to specify a custom
I'm overriding a property in my derived class that I would like to make
Message is 1024: Overriding a function that is not marked for override. what does

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.