Microsoft KB Archive/183332

= BUG: TreeView Expand Event Does Not Occur When Using ENTER Key =

Article ID: 183332

Article Last Modified on 6/29/2004

-

APPLIES TO


 * Microsoft Visual Basic 4.0 Standard Edition
 * Microsoft Visual Basic 4.0 Professional Edition
 * Microsoft Visual Basic 4.0 32-Bit Enterprise Edition
 * Microsoft Visual Basic 5.0 Learning Edition
 * Microsoft Visual Basic 5.0 Professional Edition
 * Microsoft Visual Basic 5.0 Enterprise Edition

-



This article was previously published under Q183332



SYMPTOMS
The Expand event of a TreeView control does not occur when the ENTER key is pressed to expand a node. Although the node actually expands as expected, the Expand event is not triggered.



CAUSE
This problem only occurs when the mouse is used to collapse the node (either by clicking the + sign or by double-clicking the node), then the ENTER key is pressed to expand the node. If the ENTER key is used to both collapse and expand the node, the problem does not occur.



RESOLUTION
A workaround in Visual Basic 5.0 is to trap for the ENTER key in the KeyDown event of the TreeView control and then call the Expand event. See the "Steps to Reproduce Behavior" section below for an example. This workaround is possible only for Visual Basic 5.0 because the TreeView control in Visual Basic 4.0 does not allow you to capture the ENTER key in the KeyDown event of the TreeView control.

For more information, please see the following article in the Microsoft Knowledge Base:

150200 BUG: TreeView Control Does Not Receive Key Events



STATUS
Microsoft has confirmed this to be a bug in the Microsoft products listed at the beginning of this article. We are researching this bug and will post new information here in the Microsoft Knowledge Base as it becomes available.



Steps to Reproduce Behavior
 Start a new Standard EXE project in Visual Basic 5.0. Form1 is created by default. On the Project menu, click Components, and select "Microsoft Windows Common Controls 5.0." Place a TreeView control on Form1.  Copy and paste the following code to the Form1 module: Option Explicit

Private Sub Form_Load With TreeView1 .LineStyle = tvwRootLines ' Linestyle 1 .Nodes.Add, , "r", "Root" .Nodes.Add "r", tvwChild, "c1", "Child" End With End Sub

Private Sub TreeView1_Expand(ByVal Node As ComctlLib.Node) MsgBox "TreeView1_Expand" End Sub  Run the project.</li> Press the ENTER key to expand the Root node. Notice the message box appears indicating that the Expand event has occurred.</li> Use the mouse to collapse the Root tree by clicking the minus (-) symbol next to Root. (It is important to use the mouse because the problem occurs only when using the mouse to collapse the node.)</li> Press the ENTER key.

RESULT: The message box does not appear because the Expand event does not occur.</li></ol>

Workaround
Replace the code in the Form1 module with the following code: Option Explicit

Private IsExpanded As Boolean

Private Sub Form_Load With TreeView1 .LineStyle = tvwRootLines ' Linestyle 1 .Nodes.Add, , "r", "Root" .Nodes.Add "r", tvwChild, "c1", "Child" End With End Sub

Private Sub TreeView1_Collapse(ByVal Node As ComctlLib.Node) IsExpanded = False End Sub

Private Sub TreeView1_Expand(ByVal Node As ComctlLib.Node) If IsExpanded Then Exit Sub MsgBox "TreeView1_Expand" IsExpanded = True End Sub

Private Sub TreeView1_KeyDown(KeyCode As Integer, Shift As Integer) If Not TreeView1.SelectedItem.Expanded Then Exit Sub If KeyCode = 13 Then TreeView1_Expand TreeView1.SelectedItem End Sub When you run the project, the KeyDown event of the TreeView control determines if the ENTER key was pressed. If the ENTER key was pressed, code in the KeyDown event calls the Expand event of the TreeView control. The IsExpanded flag variable is used so that the Expand event code is not executed more than once.

Keywords: kbbug kbpending kbcode KB183332

-

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

© Microsoft Corporation. All rights reserved.