2
Reply

TreeView Control DrawNode Event Problem

cris evan

cris evan

Nov 8 2010 2:29 AM
14.3k

Hi,

 I have a TreeView in one form.  I have used the DrawNode event to create two columns in the TreeView.  Source code, which is taken from microsoft msdn site, used is given below:

   private void tvwCOA_DrawNode(object sender, DrawTreeNodeEventArgs e)
        {
            int intFlag = 0;

            // Draw the background and node text for a selected node.
            if ((e.State & TreeNodeStates.Selected) != 0)
            {
                // Draw the background of the selected node. The NodeBounds
                // method makes the highlight rectangle large enough to
                // include the text of a node tag, if one is present.
                e.Graphics.FillRectangle(Brushes.Green, NodeBounds(e.Node));
                // Retrieve the node font. If the node font has not been set,
                // use the TreeView font.
                Font nodeFont = e.Node.NodeFont;
                if (nodeFont == null) nodeFont = ((TreeView)sender).Font;

                // Draw the node text.

                e.Graphics.DrawString(e.Node.Text, nodeFont, Brushes.White, e.Bounds.X, e.Bounds.Y);
                e.Graphics.DrawString(clsCommon.Right(e.Node.Tag.ToString(), 8), nodeFont, Brushes.White, 435, e.Bounds.Y);
                intFlag = 1;
            }

            // Use the default background and node text.
            else
            {
                e.DrawDefault = true;
            }

            // If a node tag is present, draw its string representation
            // to the right of the label text.

                if (intFlag != 1)
                {
                    e.Graphics.DrawString(clsCommon.Right(e.Node.Tag.ToString(), 8), tagFont, Brushes.Black, 435, e.Bounds.Top);
                }

            // If the node has focus, draw the focus rectangle large, making
            // it large enough to include the text of the node tag, if present.
                if ((e.State & TreeNodeStates.Focused) != 0)
                {
                    using (Pen focusPen = new Pen(Color.Black))
                    {
                        focusPen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot;
                        Rectangle focusBounds = NodeBounds(e.Node);
                        focusBounds.Size = new Size(focusBounds.Width - 1,
                        focusBounds.Height - 1);
                        e.Graphics.DrawRectangle(focusPen, focusBounds);
                    }
                }
        }

 

  private Rectangle NodeBounds(TreeNode node)
        {
            // Set the return value to the normal node bounds.
            Rectangle bounds = node.Bounds;
            if (node.Tag != null)
            {
                // Retrieve a Graphics object from the TreeView handle
                // and use it to calculate the display width of the tag.
                Graphics g = tvwCOA.CreateGraphics();
                //int tagWidth = (int)g.MeasureString (node.Tag.ToString(), tagFont).Width + 6;
                int tagWidth = 485; // (int)g.MeasureString(node.Text.ToString(), tagFont).Width + 6;
                // Adjust the node bounds using the calculated value.
                bounds.Offset(tagWidth / 2, 0);
                bounds = Rectangle.Inflate(bounds, tagWidth / 2, 0);
                g.Dispose();
            }

            return bounds;
        }

Each node has a value in the tag.  The problem I am facing is whenever I click a node to expand it,  the tag value of the first node in the TreeView ( which is printed against the first node at X= 435 ) is overwritten by the tag value of the first child of the expanded node. If we scroll down and come to the first node the value is correct. I tried a lot but could not locate where I am making a mistake.  Hope I explained well.  Please help.

Cris Evan


Answers (2)