Microsoft KB Archive/196775

= PRB: TreeView SelectedItem Property Does Not Return Correct Node =

Article ID: 196775

Article Last Modified on 5/13/2003

-

APPLIES TO


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

-



This article was previously published under Q196775



SYMPTOMS
When you try to drag a node from a TreeView control, the SelectedItem property does not point to the correct node.



CAUSE
The SelectedItem property of a TreeView control is not updated until the MouseUp event is fired. This means that in the MouseDown and OLEStartDrag events, the SelectedItem property returns the node that was previously selected.

This behavior is by design so that the user can select a node and then move the mouse off that node if he or she decides not to select that node. The item is not officially selected until the MouseUp event is fired.



RESOLUTION
To work around this behavior, keep track of the currently selected item manually rather than relying on the SelectedItem property. Use the HitTest method in the MouseDown event to specify this node, as shown in step 9 of the "More Information" section of this article.



Steps to Reproduce the Behavior
 Create a new Standard EXE project. By default, Form1 is created. On the Project menu, click Components, click to select the Microsoft Windows Common Controls 6.0 check box, and then click OK. Add a TreeView and a TextBox control to Form1. Set the OLEDragMode property of the TreeView control to ccOLEDragAutomatic. Set the OLEDropMode property of the TextBox to Automatic.  Add the following code to the General Declarations section of Form1: Private Sub Form_Load TreeView1.Nodes.Add, , , "Node1" TreeView1.Nodes.Add, , , "Node2" TreeView1.Nodes.Add, , , "Node3" End Sub

Private Sub TreeView1_OLEStartDrag(Data As _      MSComctlLib.DataObject, AllowedEffects As Long) AllowedEffects = vbDropEffectCopy Data.SetData TreeView1.Nodes _ (TreeView1.SelectedItem.Index).Text, vbCFText End Sub </li> Save and then run the project.</li> Click the mouse button down on either Node2 or Node3, and then drag the node text to the text box without letting the mouse button up until the cursor is over the text box.

RESULT: The item that is dragged is not the intended item; instead, it is the item that had focus before you tried to drag it.</li>  To work around this, store the current node in a private variable, and then use that node instead of the SelectedItem property. Replace the code in Form1 with the following to test the workaround: Private CurrentNode As Node

Private Sub Form_Load TreeView1.Nodes.Add, , , "Node1" TreeView1.Nodes.Add, , , "Node2" TreeView1.Nodes.Add, , , "Node3" End Sub

Private Sub TreeView1_MouseDown(Button As Integer, _      Shift As Integer, x As Single, y As Single) Set CurrentNode = TreeView1.HitTest(x, y)        CurrentNode.Selected = True End Sub

Private Sub TreeView1_OLEStartDrag(Data As _      MSComctlLib.DataObject, AllowedEffects As Long) AllowedEffects = vbDropEffectCopy Data.SetData TreeView1.Nodes(CurrentNode.Index).Text, vbCFText End Sub </li> Click the mouse button down on either Node2 or Node3, and then drag the node text to the Text1 box without letting the mouse button up until the cursor is over Text1. Notice that the intended item is plaed in the Text1 box.</li></ol>

Keywords: kbprb kbcode KB196775

-

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

© Microsoft Corporation. All rights reserved.