DOCUMENT:Q183332 01-APR-1998 [vbwin] TITLE :BUG: TreeView Expand Event Does Not Occur When Using ENTER Key PRODUCT :Microsoft Visual Basic for Windows PROD/VER:WINDOWS:4.0,5.0 OPER/SYS:WINDOWS KEYWORDS:VBKBCtrl kbcode ====================================================================== --------------------------------------------------------------------- The information in this article applies to: - Microsoft Visual Basic Standard, Professional, and Enterprise Editions, 32-bit only, for Windows, version 4.0 - Microsoft Visual Basic Learning, Professional, and Enterprise Editions for Windows, version 5.0 --------------------------------------------------------------------- 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: ARTICLE-ID: Q150200 TITLE : 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. MORE INFORMATION ================ Steps to Reproduce Behavior --------------------------- 1. Start a new Standard EXE project in Visual Basic 5.0. Form1 is created by default. 2. On the Project menu, click Components, and select "Microsoft Windows Common Controls 5.0." 3. Place a TreeView control on Form1. 4. 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 5. Run the project. 6. Press the ENTER key to expand the Root node. Notice the message box appears indicating that the Expand event has occurred. 7. 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.) 8. Press the ENTER key. Result: The message box does not appear because the Expand event does not occur. 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 : VBKBCtrl kbcode Version : WINDOWS:4.0,5.0 Platform : WINDOWS Issue type : kbbug Solution Type : kbpending ============================================================================= THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY. Copyright Microsoft Corporation 1998.