BUG: Wrong TreeView Node Selected When SingleSel Property = True

ID: Q248416


The information in this article applies to:
  • Microsoft Visual Basic Professional and Enterprise Editions, 32-bit only, for Windows, version 6.0


SYMPTOMS

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.


RESOLUTION

To work around the problem set the SingleSel property to False and implement the SingleSel functionality with your code.


STATUS

Microsoft has confirmed this to be a bug in the Microsoft products listed at the beginning of this article.


MORE INFORMATION

Steps to Reproduce Behavior

  1. Create a new Visual Basic 6.0 SP3 Standard EXE project. Form1 is created by default.


  2. On the Project menu, select Components. In the Components dialog box, select "Microsoft Windows Common Control 6.0(SP3)" and then click OK.


  3. Add a TreeView control to Form1, retaining the default name, Treeview1.


  4. Add the following code to the General Declarations section of Form1:


  5. 
    Option Explicit
    
    Const CHILD1 = "Child1"
    Const CHILD2 = "Child2"
    Const CHILD3 = "Child3"
    
    Private Sub Form_Load()
       Dim strRoot As String
       Dim strChild As String
       Dim i As Long
       
       ' Set Treeview control properties.
       TreeView1.LineStyle = tvwRootLines  ' Linestyle 1
       TreeView1.SingleSel = True  ' Single Selection
       
       ' populate the treeview control
       For i = Asc("A") To Asc("Z")
          strRoot = Chr(i)
          Call TreeView1.Nodes.Add(, , strRoot, "Root-" & strRoot)
    
          Call TreeView1.Nodes.Add(strRoot, tvwChild, strRoot & "1", CHILD1)
          Call TreeView1.Nodes.Add(strRoot, tvwChild, strRoot & "2", CHILD2)
          Call TreeView1.Nodes.Add(strRoot, tvwChild, strRoot & "3", CHILD3)
    
          strChild = strRoot & "1"
          Call TreeView1.Nodes.Add(strChild, tvwChild, strChild & "1", CHILD1)
          Call TreeView1.Nodes.Add(strChild, tvwChild, strChild & "2", CHILD2)
          Call TreeView1.Nodes.Add(strChild, tvwChild, strChild & "3", CHILD3)
    
          strChild = strRoot & "2"
          Call TreeView1.Nodes.Add(strChild, tvwChild, strChild & "1", CHILD1)
          Call TreeView1.Nodes.Add(strChild, tvwChild, strChild & "2", CHILD2)
          Call TreeView1.Nodes.Add(strChild, tvwChild, strChild & "3", CHILD3)
    
          strChild = strRoot & "3"
          Call TreeView1.Nodes.Add(strChild, tvwChild, strChild & "1", CHILD1)
          Call TreeView1.Nodes.Add(strChild, tvwChild, strChild & "2", CHILD2)
          Call TreeView1.Nodes.Add(strChild, tvwChild, strChild & "3", CHILD3)
       Next
    End Sub 
  6. Run the application by pressing F5. Select a child of "Root-A" in the third level.


  7. Select "Root-I". Note that "Root-L" is selected and expanded instead of "Root-I". This behavior is incorrect.


Steps to Work Around the Problem

  1. In the Form_Load() sub, replace the following line:


  2. 
       TreeView1.SingleSel = True  ' Single Selection 
    with
    
       TreeView1.SingleSel = False  ' Not Single Selection 
  3. Append the following code to the General Declarations section of Form1:


  4. 
    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 
  5. Run the application by pressing F5. It now works as expected.


Additional query words:

Keywords : kbsample kbCtrl kbTreeView kbVBp kbVBp600bug kbGrpVB kbDSupport
Version : WINDOWS:6.0
Platform : WINDOWS
Issue type : kbbug


Last Reviewed: January 4, 2000
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.