When using the Microsoft Visual Basic 6.0 service pack 3 (SP3) version of the TreeView Control, in a project with the SingleSel property set to True, when you click a node sometimes a different node will be selected and expanded.
Microsoft has confirmed that this is a bug in the Microsoft products that are listed at the beginning of this article. This bug was corrected in the latest service pack for Visual Studio 6.0.
For additional information about Visual Studio service packs, click the following article numbers to view the articles in the Microsoft Knowledge Base:
194022
(http://support.microsoft.com/kb/194022/EN-US/
)
INFO: Visual Studio 6.0 Service Packs, What, Where, Why
194295
(http://support.microsoft.com/kb/194295/EN-US/
)
HOWTO: Tell That a Visual Studio Service Pack Is Installed
To download the latest Visual Studio service pack, visit the following Microsoft Web site:
In the Form_Load() sub, replace the following line:
TreeView1.SingleSel = True ' Single Selection
with
TreeView1.SingleSel = False ' Not Single Selection
Append the following code to the General Declarations section of Form1:
Private Sub TreeView1_NodeClick(ByVal node As MSComctlLib.node)
Call SimulateSingleSel(node)
End Sub
Private Sub SimulateSingleSel(ByVal node As MSComctlLib.node)
' expand current node
node.Expanded = True
' collapse all siblings and parents' siblings
Dim nodeParent As MSComctlLib.node
Set nodeParent = node
Do Until nodeParent Is Nothing
CollapseSiblings nodeParent
Set nodeParent = nodeParent.Parent
Loop
' collapse child's siblings (collapse all children)
Dim nodeChild As MSComctlLib.node
Set nodeChild = node.Child
If nodeChild Is Nothing Then Exit Sub
nodeChild.Expanded = False
Call CollapseSiblings(nodeChild)
End Sub
Private Sub CollapseSiblings(ByVal node As MSComctlLib.node)
Dim n As Integer
n = node.FirstSibling.Index
If n <> node.Index Then node.FirstSibling.Expanded = False
Do While n <> node.LastSibling.Index
n = TreeView1.Nodes(n).Next.Index
If n <> node.Index Then TreeView1.Nodes(n).Expanded = False
Loop
End Sub
Run the application by pressing F5. It now works as expected.