VB.Net: Setting the SelectedNode and SelectedItem font in Treeview and Listview Controls
-
Hi guys, I am struggling to change the font style of a selected node in a treeview and a selected item in a listview control, your help is very much appreciated. Regards:~
Got it and a bit embarrassed, its amazingly simple. here are my lines, I used the
**AfterSelect**
event of the TreeView to set the font of the node, its parent and all its ancestors to bold, when another node is selected, i set the new node and its ancestors to bold, while returning the previously selected node to the normal font of the TreeView . By removing thewhile...
statement in both methods, only the selected node is set to bold and back to normal. I know there are most likely better solutions but this one works fine.Dim previousSelectedNode as TreeNode ... ... Private Sub Treeview1_AfterSelect(ByVal sender As System.Object, ByVal e As _ System.Windows.Forms.TreeViewEventArgs) Handles Treeview1.AfterSelect Dim xnode As TreeNode If previousSelectedNode IsNot Nothing Then DeSelectNode() e.Node.NodeFont = New Font(Treeview1.Font, FontStyle.Bold) xnode = e.Node While xnode.Parent IsNot Nothing xnode = xnode.Parent xnode.NodeFont = New Font(Treeview1.Font, FontStyle.Bold) End While previousSelectedNode = e.Node End Sub
'Deselecting a node Private Sub DeSelectNode() previousSelectedNode.NodeFont = Treeview1.Font While previousSelectedNode.Parent IsNot Nothing previousSelectedNode = previousSelectedNode.Parent previousSelectedNode.NodeFont = Treeview1.Font End While End Sub