Microsoft KB Archive/248416

= FIX: Wrong TreeView Node Selected When SingleSel Property = True =

Article ID: 248416

Article Last Modified on 8/7/2007

-

APPLIES TO


 * Microsoft Visual Basic 6.0 Professional Edition
 * Microsoft Visual Basic 6.0 Enterprise Edition

-



This article was previously published under Q248416



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 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 INFO: Visual Studio 6.0 Service Packs, What, Where, Why

194295 HOWTO: Tell That a Visual Studio Service Pack Is Installed

To download the latest Visual Studio service pack, visit the following Microsoft Web site:

http://msdn2.microsoft.com/en-us/vstudio/Aa718353.aspx



Steps to Reproduce Behavior
 Create a new Visual Basic 6.0 SP3 Standard EXE project. Form1 is created by default. On the Project menu, select Components. In the Components dialog box, select "Microsoft Windows Common Control 6.0(SP3)" and then click OK. Add a TreeView control to Form1, retaining the default name, Treeview1.  Add the following code to the General Declarations section of Form1: 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  Run the application by pressing F5. Select a child of "Root-A" in the third level. Select "Root-I". Note that "Root-L" is selected and expanded instead of "Root-I". This behavior is incorrect.</li></ol>

Steps to Work Around the Problem
<ol>  In the Form_Load sub, replace the following line: TreeView1.SingleSel = True ' Single Selection with TreeView1.SingleSel = False ' Not Single Selection </li>  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 </li> Run the application by pressing F5. It now works as expected.</li></ol>

Additional query words: sp4

Keywords: kbbug kbctrl kbfix kbtreeview kbvs600sp4fix kbvs600sp5fix KB248416

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.