Step 4: Custom Mouse and Keyboard Handling

The VSFlexGrid control provides the expanding and collapsing for you, but you may extend and customize its behavior. Every time a branch is expanded or collapsed, the control fires the BeforeCollapse and AfterCollapse events so you may take actions in response to that. Furthermore, you may use the IsCollapsed property to get and set the collapsed state of each branch in code.

For example, the following code allows users to expand and collapse outline branches by double-clicking on a row itself, rather than on the outline bar. Here's the code to do it:

    Private Sub fg_DblClick()

        Dim r As Long

        With fg

            r = .Row

            If .IsCollapsed(r) = flexOutlineCollapsed Then

                .IsCollapsed(r) = flexOutlineExpanded

            Else

                .IsCollapsed(r) = flexOutlineCollapsed

            End If

        End With

    End Sub

The code checks the current row. If it is collapsed, then it expands it. Otherwise, it collapses it. Collapsing a detail row collapses its entire parent node.

We can use the same code to implement the keyboard interface. We just call the DblClick event handler from the KeyPress handler:

    Private Sub fg_KeyPress(KeyAscii As Integer)

        If KeyAscii = vbKeyReturn Then fa_DblClick

    End Sub

This closes the Outline demo. Press F5 to run the project one last time and test the additional mouse and keyboard handling.