none
OwnerDrawText TreeNode repeating text

    Question

  • Hello all,

    I found a rather annoying bug when using a TreeView with the DrawMode set to OwnerDrawText.  I wanted to add a button to some child nodes, but it appears when I open one or two such parent nodes, the text on the button is written again on the top of the TreeNode.  Any thoughts?  My code follows:

            public Form1()
            {
    
                InitializeComponent();
    
                treeView1.DrawMode = TreeViewDrawMode.OwnerDrawText;
                treeView1.DrawNode += new DrawTreeNodeEventHandler(treeView1_DrawNode);
    
                treeView1.Nodes.Add("Parent1");
                treeView1.Nodes[treeView1.Nodes.Count - 1].Nodes.Add("Child1");
                treeView1.Nodes[treeView1.Nodes.Count - 1].Nodes.Add("Child2");
                treeView1.Nodes[treeView1.Nodes.Count - 1].Nodes.Add("Custom");
                treeView1.Nodes.Add("Parent2");
                treeView1.Nodes[treeView1.Nodes.Count - 1].Nodes.Add("Child3");
                treeView1.Nodes[treeView1.Nodes.Count - 1].Nodes.Add("Child4");
                treeView1.Nodes[treeView1.Nodes.Count - 1].Nodes.Add("Custom");
    
            }
    
            void treeView1_DrawNode(object sender, DrawTreeNodeEventArgs e)
            {
                if (e.Node.Text == "Custom")
                {
    
                    System.Drawing.Rectangle cr = e.Bounds;
    
                    Font fnt = new Font("Arial", 7, System.Drawing.FontStyle.Italic);
    
                    ButtonRenderer.DrawButton(e.Graphics, cr, e.Node.Text, fnt, false, PushButtonState.Default);
    
                }
                else
    
                    e.DrawDefault = true;
            }
    

    Thank you in advance.
    • Moved by CoolDadTxMVP Wednesday, April 04, 2012 1:43 PM Winforms related (From:Visual C# General)
    Wednesday, April 04, 2012 1:31 PM

Answers

  • Jason,

    The ImageIndex change didn't work for me, either.

    Also, I tried using VS 2008 to make a dll referenced by my VS 2010 form.  That didn't help either, FYI.

    What does seem to help, however, is this:

            void treeView1_AfterExpand(object sender, TreeViewEventArgs e)
            {
                treeView1.Refresh();
            }
    

    Hannes,

    Thanks for pointing me in the right direction.  I hope you understand I can't exactly mark it as the answer.

    • Marked as answer by trufaux Thursday, April 05, 2012 1:28 PM
    Thursday, April 05, 2012 1:28 PM

All replies

  • Hi,

    sorry i can`t help you with providing a solution. But i have tested this with both C# 2008 and C# 2010. For C# 2010 i can see the effect you have described, but for C# 2008 everything is like it should. This seems to be some kind of refreshing error, because if you e.g minimize your form after you see this text and then show it again, the text is gone and won`t return.


    Hannes

    If you have got questions about this, just ask.

    In a perfect world,
    users would never enter data in the wrong form,
    files they choose to open would always exist
    and code would never have bugs.

    C# to VB.NET: http://www.developerfusion.com/tools/convert/csharp-to-vb/

    Wednesday, April 04, 2012 2:10 PM
  • Hi trufaux,

      I have used your another override method,its official definition is as follows:

    http://msdn.microsoft.com/en-us/library/yks43619.aspx

      I use it ImageIndex property inside the node of DrawTreeNodeEventArgs.its functionality looks like tag inside those control. you can follow this snippet of code to change you original code"

    public Form1() { InitializeComponent(); treeView1.DrawMode = TreeViewDrawMode.OwnerDrawText; treeView1.DrawNode += new DrawTreeNodeEventHandler(treeView1_DrawNode); treeView1.Nodes.Add("Parent1"); treeView1.Nodes[treeView1.Nodes.Count - 1].Nodes.Add("Child1"); treeView1.Nodes[treeView1.Nodes.Count - 1].Nodes.Add("Child2"); treeView1.Nodes[treeView1.Nodes.Count - 1].Nodes.Add("Custom", "Custom",1); treeView1.Nodes.Add("Parent2"); treeView1.Nodes[treeView1.Nodes.Count - 1].Nodes.Add("Child3"); treeView1.Nodes[treeView1.Nodes.Count - 1].Nodes.Add("Child4"); treeView1.Nodes[treeView1.Nodes.Count - 1].Nodes.Add("Custom", "Custom",2); } void treeView1_DrawNode(object sender, DrawTreeNodeEventArgs e) { if (e.Node.ImageIndex== 1) { System.Drawing.Rectangle cr = e.Bounds; Font fnt = new Font("Arial", 7, System.Drawing.FontStyle.Italic); ButtonRenderer.DrawButton(e.Graphics, cr, e.Node.Text, fnt, false, PushButtonState.Default); } else e.DrawDefault = true; }

    Hope it will help you.

    Sincerely,

    Jason Wang


    Jason Wang [MSFT]
    MSDN Community Support | Feedback to us

    Thursday, April 05, 2012 7:21 AM
  • Jason,

    if you change the if condition to

    if (e.Node.ImageIndex== 2)

    or

    if (e.Node.ImageIndex != -1)

    it will result in the same behavior.

    Hannes

    If you have got questions about this, just ask.

    In a perfect world,
    users would never enter data in the wrong form,
    files they choose to open would always exist
    and code would never have bugs.

    C# to VB.NET: http://www.developerfusion.com/tools/convert/csharp-to-vb/

    Thursday, April 05, 2012 8:10 AM
  • Jason,

    The ImageIndex change didn't work for me, either.

    Also, I tried using VS 2008 to make a dll referenced by my VS 2010 form.  That didn't help either, FYI.

    What does seem to help, however, is this:

            void treeView1_AfterExpand(object sender, TreeViewEventArgs e)
            {
                treeView1.Refresh();
            }
    

    Hannes,

    Thanks for pointing me in the right direction.  I hope you understand I can't exactly mark it as the answer.

    • Marked as answer by trufaux Thursday, April 05, 2012 1:28 PM
    Thursday, April 05, 2012 1:28 PM