Article ID: 183332
Article Last Modified on 6/29/2004
150200 BUG: TreeView Control Does Not Receive Key Events
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
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