Click here to Skip to main content
15,884,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi

i am using the below code for drawing my tree node

i want two draw it for all sub child node .

step by step i.e if Drawing is complet for one child than only it drw for next child please help me on this.


C#
private void treeViewExec_DrawNode(object sender, DrawTreeNodeEventArgs e)
    {

            if ((e.Node == treeViewExec.Nodes[0].Nodes[0]))
            {
                using (Font font = new Font(this.Font, FontStyle.Regular))
                {
                    using (Brush brush = new SolidBrush(Color.Black))
                    {
                        if (m_strParentNodeText == "")
                        {
                            e.Graphics.DrawString(treeViewExec.Nodes[0].Nodes[0].Text + "-(", font, brush, e.Bounds.Left, e.Bounds.Top);
                        }
                        else
                        {
                            e.Graphics.DrawString(m_strParentNodeText + "-(", font, brush, e.Bounds.Left, e.Bounds.Top);
                        }
                    }
                    using (Brush brush = new SolidBrush(Color.Green))
                    {

                            e.Graphics.DrawString(m_nTestcasePass.ToString(), font, brush, e.Bounds.Left + 35, e.Bounds.Top);

                    }
                    using (Brush brush = new SolidBrush(Color.Black))
                    {
                        e.Graphics.DrawString(")", font, brush, e.Bounds.Left + 49, e.Bounds.Top);
                    }
                    using (Brush brush = new SolidBrush(Color.Black))
                    {
                        e.Graphics.DrawString(",", font, brush, e.Bounds.Left + 40, e.Bounds.Top);
                    }
                    using (Brush brush = new SolidBrush(Color.Red))
                    {
                        SizeF s = e.Graphics.MeasureString(m_nTestcasePass.ToString(), font);
                        e.Graphics.DrawString(m_nTestcaseFailed.ToString(), font, brush, e.Bounds.Left + 35 + (int)s.Width, e.Bounds.Top);
                    }
                }
            }
            else
            {
                e.DrawDefault = true;

            }
        }
Posted
Updated 3-Mar-13 22:47pm
v2
Comments
Jegan Thiyagesan 4-Mar-13 5:24am    
So what is the problem?
where are you getting stuck?
What feature are you missing?

1 solution

Hi,
have a try on the code below, it seems working but missing the tree line of the first node, I am not sure why.

C#
private void treeViewExec_DrawNode(object sender, DrawTreeNodeEventArgs e)
        {
            if (e.Node == treeViewExec.Nodes[0].Nodes[0])
            {
                TreeNode node = treeViewExec.Nodes[0].Nodes[0];
                do
                {
                    int leftBound = node.Bounds.Left ;
                    string str = "";
                    if (m_strParentNodeText == "")
                    {
                        str = node.Text + "-(";
                    }
                    else
                    {
                        str = m_strParentNodeText + "-(";
                    }

                    using (Font font = new Font(this.Font, FontStyle.Regular))
                    {
                        DoDrawing(str, e.Graphics, font, Color.Black, leftBound, node.Bounds.Top);

                        leftBound = CalculateLeftBoundry(str, e.Graphics, leftBound, font);
                        DoDrawing(m_nTestcasePass.ToString(), e.Graphics, font, Color.Green, leftBound, node.Bounds.Top);

                        leftBound = CalculateLeftBoundry(m_nTestcasePass.ToString(), e.Graphics, leftBound, font);
                        DoDrawing(")", e.Graphics, font, Color.Black, leftBound, node.Bounds.Top);

                        leftBound = CalculateLeftBoundry(")", e.Graphics, leftBound, font);
                        DoDrawing(",", e.Graphics, font, Color.Black, leftBound, node.Bounds.Top);

                        leftBound = CalculateLeftBoundry(",", e.Graphics, leftBound, font);
                        DoDrawing(m_nTestcaseFailed.ToString(), e.Graphics, font, Color.Red, leftBound, node.Bounds.Top);
                    }
                } while ((node = node.NextNode) != null);
            }
            else
            {
                e.DrawDefault = true;

            }
        }

        private int CalculateLeftBoundry(string text, Graphics graphics, int currentLeftBound, Font font)
        {
            SizeF s = graphics.MeasureString(text, font);
            return (currentLeftBound + (int)s.Width);
        }

        private void DoDrawing(string text, Graphics graphics, Font font, Color color, int leftBoundry, int topBoundry)
        {
            using (Brush brush = new SolidBrush(color))
            {
                graphics.DrawString(text, font, brush, leftBoundry, topBoundry);
            }
        }


It seems to display all child nodes in the colour you wanted. Also I have refactored your code to make more readable.

Regards
Jegan
 
Share this answer
 
Comments
rahuls1 4-Mar-13 7:09am    
ya i am abel to draw the node but the problem is that Suppose i ahve two child node and this two chil node as sub child node so what i have to do is when my excution start suppose i complet the excution of of first child node i draw that chil node with two differnet color and when my excution a gain goes to second chil node it should draw second child node not the first child node .
Jegan Thiyagesan 4-Mar-13 7:19am    
my code above should work for every sibling in that node tree.
See line "while ((node = node.NextNode) != null);"

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900