PRB: TreeView SelectedItem Property Does Not Return Correct Node
ID: Q196775
|
The information in this article applies to:
-
Microsoft Visual Basic Learning, Professional, and Enterprise Editions for Windows, versions 5.0, 6.0
SYMPTOMS
When you attempt 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. It allows the user the flexibility to 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
The workaround for this behavior is to 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 below.
MORE INFORMATION- Create a new Standard EXE project. Form1 is created by default.
- From the Project menu, click Components, select the Microsoft Windows
Common Controls 6.0 check box, and click OK.
- Add a TreeView and a TextBox control to Form1.
- Set the OLEDragMode property of the TreeView control to
ccOLEDragAutomatic.
- Set the DropMode 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
- Save the project and run it.
- Click the mouse button down on either Node2 or Node3 and drag the node
text to the TextBox without letting the mouse button up until the cursor
is over the TextBox.
RESULT: The item that is dragged is not the intended item, instead it is
the item that had focus before you attempted to drag it.
- To work around this, to store the current node in a private variable and
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
- Click the mouse button down on either Node2 or Node3 and drag the node
text to the Text1 without letting the mouse button up until the cursor
is over the Text1.
RESULT: The intended item is placed in Text1.
Keywords : kbole kbVBp500 kbVBp600 kbGrpVBDB kbCodeSam
Version : WINDOWS:5.0,6.0
Platform : WINDOWS
Issue type : kbprb
|