I'm making an interface to edit scenarios for a game. Basically it consists of events, which have nested conditions and actions. So, I planned on using two treeviews - one for selecting the event, and other for selecting the condition/action inside the event to edit.
Now, you see, if I select an event (in left treeview) and then try to select something in the right treeview, the left treeview will stop showing the blue selection rectangle. This is obviously bad because now the user doesn't know which event is he editing!
The only way I found to retain some sort of information about what is the current selection is by using SelectedImageIndex, but that's only one little image that will be different.
Is there any other way to highlight the treenode while there is no focus on the treeview? I know I can just use Graphics.DrawRectangle or something, but I heard that drawing should be done in Paint event and treeview has no paint event... So I guess if I draw it on the event of losing focus, and then drag the form out of the screen or something, it will be "erased"?
Anyway, please tell me if you got an idea (other than using a separate icon for selected and not selected treenode).
C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...
What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.
In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.
Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.
What you are looking for is the HideSelection
property on the TreeView
.
From MSDN:
Gets or sets a value indicating whether the selected tree node remains highlighted even when the tree view has lost the focus.
Link: http://msdn.microsoft.com/en-us/library/system.windows.forms.treeview.hideselection.aspx
Code:
TreeView.HideSelection = false;
It is still shown but only in light grey which depending on your screen and current setup can be near in visible!
Override the OnDrawNode event. So you create and new class (call it "SpecialTreeView") an inherit from the Microsoft TreeView
like class SpecialTreeView : TreeView
. Then you add the following event override:
protected override void OnDrawNode(DrawTreeNodeEventArgs e) { TreeNodeStates treeState = e.State; Font treeFont = e.Node.NodeFont ?? e.Node.TreeView.Font; // Colors. Color foreColor = e.Node.ForeColor; string strDeselectedColor = @"#6B6E77", strSelectedColor = @"#94C7FC"; Color selectedColor = System.Drawing.ColorTranslator.FromHtml(strSelectedColor); Color deselectedColor = System.Drawing.ColorTranslator.FromHtml(strDeselectedColor); // New brush. SolidBrush selectedTreeBrush = new SolidBrush(selectedColor); SolidBrush deselectedTreeBrush = new SolidBrush(deselectedColor); // Set default font color. if (foreColor == Color.Empty) foreColor = e.Node.TreeView.ForeColor; // Draw bounding box and fill. if (e.Node == e.Node.TreeView.SelectedNode) { // Use appropriate brush depending on if the tree has focus. if (this.Focused) { foreColor = SystemColors.HighlightText; e.Graphics.FillRectangle(selectedTreeBrush, e.Bounds); ControlPaint.DrawFocusRectangle(e.Graphics, e.Bounds, foreColor, SystemColors.Highlight); TextRenderer.DrawText(e.Graphics, e.Node.Text, treeFont, e.Bounds, foreColor, TextFormatFlags.GlyphOverhangPadding); } else { foreColor = SystemColors.HighlightText; e.Graphics.FillRectangle(deselectedTreeBrush, e.Bounds); ControlPaint.DrawFocusRectangle(e.Graphics, e.Bounds, foreColor, SystemColors.Highlight); TextRenderer.DrawText(e.Graphics, e.Node.Text, treeFont, e.Bounds, foreColor, TextFormatFlags.GlyphOverhangPadding); } } else { if ((e.State & TreeNodeStates.Hot) == TreeNodeStates.Hot) { e.Graphics.FillRectangle(SystemBrushes.Window, e.Bounds); TextRenderer.DrawText(e.Graphics, e.Node.Text, hotFont, e.Bounds, System.Drawing.Color.Black, TextFormatFlags.GlyphOverhangPadding); } else { e.Graphics.FillRectangle(SystemBrushes.Window, e.Bounds); TextRenderer.DrawText(e.Graphics, e.Node.Text, treeFont, e.Bounds, foreColor, TextFormatFlags.GlyphOverhangPadding); } } }
Compile the code and you should see "SpecialTreeView" in your tool box in the designer. Replace your TreeView with this new one using the same name and the only thing that will be different is the selection colours. When selected it will be selectedColor
, when not selected the deselectedColor
.
I hope this helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With