views:

71

answers:

1

I set the BackColor of specific TreeNodes in a TreeView as a hint to the user that something interesting has happened to the node while they are using the application. However, when I set BackColor, it causes the entire parent TreeView control to redraw rather than just the label area of the specific TreeNode that has been changed. I am not calling Refresh or Update at any point -- just setting BackColor on the TreeNode. It seems that rather than just invalidating the bounds of the TreeNode that has been changed, the TreeView is refreshing its entire area. This results in an annoying quick flash of the control.

Any idea why this happening and if it can be easily stopped?

+1  A: 

It doesn't look like you can stop this from occuring. I took a look at the code for the TreeNode.BackColor setter:

[SRDescription("TreeNodeBackColorDescr"), SRCategory("CatAppearance")]
public Color BackColor
{
    get
    {
        if (this.propBag == null)
        {
            return Color.Empty;
        }
        return this.propBag.BackColor;
    }
    set
    {
        Color backColor = this.BackColor;
        if (value.IsEmpty)
        {
            if (this.propBag != null)
            {
                this.propBag.BackColor = Color.Empty;
                this.RemovePropBagIfEmpty();
            }
            if (!backColor.IsEmpty)
            {
                this.InvalidateHostTree();
            }
        }
        else
        {
            if (this.propBag == null)
            {
                this.propBag = new OwnerDrawPropertyBag();
            }
            this.propBag.BackColor = value;
            if (!value.Equals(backColor))
            {
                this.InvalidateHostTree();
            }
        }
    }
}

Whenever the BackColor changes, an invalidate is forced on the tree that contains the node. Again, looking at the InvalidateHostTree function, there are no flags you can set to stop the refresh from occurring.

fletcher
Thanks. That's what I feared. I guess a workable solution might be OwnerDraw with my own CustomBackColor property.
Dave76