EDN Admin
Well-known member
Using Visual C# 2010 Express, Ive created a derived TreeView that, among other custom things, ignores mouse clicks on certain nodes and creates custom highlighting for other nodes that are clicked. To achieve these results, I alter the SelectedNode property in a NodeMouseClick event handler and highlight the clickable node text in an OnDrawNode override method (see code below where ClickableNode and previousNode are what you would expect). This works as expected in all but one situation, which I am trying to debug. But in the course of debugging, Im seeing behavior I cant explain.
If I set a program stop in the NodeMouseClick event handler, I can see that this event is invoked each time I click on a node. However, if I also set a stop in the OnDrawNode override method, that method is invoked multiple times as the TreeView re-draws its nodes, but the NodeMouseClick event never happens. In both cases, I place the stops on the 1st statement in each method. Any idea whats going on?
Steve private void MyTreeView_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
{
if (ClickableNode(e.Node.Text) == false) this.SelectedNode = previousNode;
}
protected override void OnDrawNode(DrawTreeNodeEventArgs e)
{
base.OnDrawNode(e);
if (e.Node == this.SelectedNode)
{
using (SolidBrush brush = new SolidBrush(Color.AntiqueWhite))
{ e.Graphics.FillRectangle(brush, e.Node.Bounds); }
TextRenderer.DrawText(e.Graphics, e.Node.Text, e.Node.NodeFont ?? e.Node.TreeView.Font, e.Node.Bounds,
Color.Fuchsia, TextFormatFlags.Left | TextFormatFlags.Top);
}
else e.DrawDefault = true;
}
View the full article
If I set a program stop in the NodeMouseClick event handler, I can see that this event is invoked each time I click on a node. However, if I also set a stop in the OnDrawNode override method, that method is invoked multiple times as the TreeView re-draws its nodes, but the NodeMouseClick event never happens. In both cases, I place the stops on the 1st statement in each method. Any idea whats going on?
Steve private void MyTreeView_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
{
if (ClickableNode(e.Node.Text) == false) this.SelectedNode = previousNode;
}
protected override void OnDrawNode(DrawTreeNodeEventArgs e)
{
base.OnDrawNode(e);
if (e.Node == this.SelectedNode)
{
using (SolidBrush brush = new SolidBrush(Color.AntiqueWhite))
{ e.Graphics.FillRectangle(brush, e.Node.Bounds); }
TextRenderer.DrawText(e.Graphics, e.Node.Text, e.Node.NodeFont ?? e.Node.TreeView.Font, e.Node.Bounds,
Color.Fuchsia, TextFormatFlags.Left | TextFormatFlags.Top);
}
else e.DrawDefault = true;
}
View the full article