Reputation:
When I select a Node
in the TreeView
, it highlights and I show data based on that Node
. When I select another Control
(the TreeView
loses focus) it is no longer highlighted. How do I keep it highlighted after losing focus?
While doing a search I cant tell which node is selected since I must keep the focus on the textbox (so the user can type more text).
Upvotes: 8
Views: 9002
Reputation: 549
If the highlight color isn't bright enough to your liking when HideSelection is set to False and the TreeView lost focus:
Make sure your TreeView's HideSelection is set to True (default value).
Then handle the TreeView's Enter and Leave events like:
void myTreeView_Leave(object sender, EventArgs e)
{
if((sender as TreeView).SelectedNode != null)
(sender as System.Windows.Forms.TreeView).SelectedNode.BackColor = Color.Red; //your highlight color
}
void myTreeView_Enter(object sender, EventArgs e)
{
if((sender as TreeView).SelectedNode != null)
(sender as TreeView).SelectedNode.BackColor = (sender as TreeView).BackColor;
}
Upvotes: 0
Reputation: 1682
I just run into this issue and this is how I addressed it: Changed the DrawMode property to TreeViewDrawMode.OwnerDrawText
and registered to DrawNode event
private void MyTreeGridview_DrawNode(object sender, DrawTreeNodeEventArgs e)
{
if ((e.State == TreeNodeStates.Selected) && (!MyTreeGridview.Focused))
{
Font font = e.Node.NodeFont ?? e.Node.TreeView.Font;
Color fore = e.Node.ForeColor;
if (fore == Color.Empty)fore = e.Node.TreeView.ForeColor;
fore = SystemColors.HighlightText;
Color highlightColor = SystemColors.Highlight;
e.Graphics.FillRectangle(new SolidBrush(highlightColor), e.Bounds);
ControlPaint.DrawFocusRectangle(e.Graphics, e.Bounds, fore, highlightColor);
TextRenderer.DrawText(e.Graphics, e.Node.Text, font, e.Bounds, fore, highlightColor, TextFormatFlags.GlyphOverhangPadding);
}
else
{
e.DrawDefault = true;
}
}
Upvotes: 4
Reputation: 11910
You have to set the HideSelection property to false - so you'll see the selection, altough the TreeView control lost focus
Upvotes: 10