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
Suthish NairPosted Dec 3, 2010, 1:16 PM
Upload whole code as attachment, showing clsCommon error.
CrishPosted Dec 3, 2010, 4:36 AM
You can refer this following code for TreeView Control DrawNode Event Proble.
Here is the code of the drawNode-Event:
Private Sub TreeViewExt_DrawNode(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DrawTreeNodeEventArgs) Handles TreeViewExt.DrawNode
'Based on code from: http://msdn.microsoft.com/en-us/library/system.windows.forms.treeview.drawnode(VS.85).aspx
Dim nde As TreeNodeExtended = CType(e.Node, TreeNodeExtended)
' Retrieve the node font. If the node font has not been set, use the TreeView font.
Dim nodeFont As Font = e.Node.NodeFont
If nodeFont Is Nothing Then
nodeFont =
CType(sender, TreeView).Font
End If
Dim nodeFontBold As New Font(nodeFont.FontFamily.Name, nodeFont.Size - CSng(0.8), FontStyle.Bold)
Dim nodeToUse As Font
Dim brushToUseRect As Brush
Dim brushToUseFont As Brush
If nde.Properties("AuditID") <> String.Empty Then
nodeToUse = nodeFontBold
Else
nodeToUse = nodeFont
End If
If (e.State And TreeNodeStates.Selected) <> 0 Then
'selected node
brushToUseRect = Brushes.Blue
brushToUseFont = Brushes.White
Else
brushToUseRect = Brushes.White
brushToUseFont = Brushes.Black
End If
'Draw background
e.Graphics.FillRectangle(brushToUseRect, e.Node.Bounds)
' Draw text
e.Graphics.DrawString(e.Node.Text, nodeToUse, brushToUseFont, e.Node.Bounds.Left - 2, e.Node.Bounds.Top)
End Sub