8

I implemented a multicolor system for each of my TreeView nodes. But everytime I expand a child node, it expends but also paints the node over my rootNode (image 2 and 3). The code is from my previous question and this is what the bug looks like

enter image description here

If I decide to close every node and re-expand the glitch is gone.(image 4)

The problem Seems to be with the Bounds that's why the draw isn't at the right place. Any idea why ?


  private void treeView1_DrawNode(object sender, DrawTreeNodeEventArgs e)
  {
    string[] texts = e.Node.Text.Split();
    using (Font font = new Font(this.Font, FontStyle.Regular))
    {
        using (Brush brush = new SolidBrush(Color.Red))
        {
            e.Graphics.DrawString(texts[0], font, brush, e.Bounds.Left, e.Bounds.Top);
        }

        using (Brush brush = new SolidBrush(Color.Blue))
        {
            SizeF s = e.Graphics.MeasureString(texts[0], font);
            e.Graphics.DrawString(texts[1], font, brush, e.Bounds.Left + (int)s.Width, e.Bounds.Top);
        }
    }
  }
Community
  • 1
  • 1
phadaphunk
  • 12,785
  • 15
  • 73
  • 107
  • Interesting... What is the value of treeView1.DrawMode, and that of e.Bounds.Left, e.Bounds.Top, and s.Width when drawing the node? – kol Dec 11 '12 at 17:50
  • The drawMode is TreeViewDrawMode.OwnerDrawText – phadaphunk Dec 11 '12 at 17:57
  • That's correct. And what are the values of the variables I mentioned? – kol Dec 11 '12 at 17:59
  • I can't check the value. I put a breakpoint so everytime I enter the treeview it breaks before I can expand – phadaphunk Dec 11 '12 at 18:00
  • Unless you know I way I could check the value without the breakpoint – phadaphunk Dec 11 '12 at 18:00
  • Use Debug.WriteLine or Trace.WriteLine from System.Diagnostics. These print a message to VS console. – kol Dec 11 '12 at 18:05
  • Bounds Left = 21 Top = 0 //// Bounds = X = 21,Y=0 , Width = 84, Height = 18 – phadaphunk Dec 11 '12 at 18:10
  • Reviving old thread because root cause is hard to find in docs. Draw node is not only called to draw a node but also if you click expand etc. If you don't want to draw over top left corner, examine bounds and exit sub if they don't correspond with a node label location (for OwnerDrawText) – saminpa Jun 09 '18 at 15:02

2 Answers2

5

Drawing glitch seems to be an accurate description.

You can try this work around by subscribing to the AfterExpand event:

void treeView1_AfterExpand(object sender, TreeViewEventArgs e) {
  treeView1.Invalidate();
}
LarsTech
  • 80,625
  • 14
  • 153
  • 225
  • @PhaDaPhunk Invalidate() just tells the control to redraw itself. It doesn't really fix the Microsoft issue, and it might end up causing a little bit of flicker since it needs to draw all the nodes again. – LarsTech Dec 11 '12 at 19:14
  • yes I saw the flicker. But unless there is a way to fix the glitch I will do with that for the moment – phadaphunk Dec 11 '12 at 19:20
  • @PhaDaPhunk No experience if this works, but it might be worth trying: [Double-buffered Tree and List views](http://dev.nomad-net.info/articles/double-buffered-tree-and-list-views) – LarsTech Dec 11 '12 at 20:08
0

Just Make Sure To Use : "BeginUpdate()" and "EndUpdate()" Everytime You Populate the TreeView

The "EndUpdate()"<- Is the Most Important thing to Get Rid this Kind of Glitch!

Good Luck! :)

Cheers, J