BeforeSelChange Event

Fired before the selected range (RowSel, ColSel) changes.

Syntax

Private Sub VSFlexGrid_BeforeSelChange( ByVal OldRowSel As Long,  ByVal OldColSel As Long,  ByVal NewRowSel As Long,  ByVal NewColSel As Long, Cancel As Boolean)

Remarks

This event is fired before the RowSel and ColSel properties change, either as a result of user actions or through code. It allows you to prevent the selection of certain cells, thus creating "protected" ranges on a grid.

To prevent the selection of a range, you also need to handle the BeforeRowColChange event, which is fired before the Row and Col properties change.

For example, the following code creates a protected range with a green background and prevents the user from selecting any cells on the protected range and from extending any selections into the protected area:

    ' highlight protected range

    Private Sub Form_Load()

        fg.Cell(flexcpBackColor, 2, 2, 8, 4) = RGB(200, 250, 200)

    End Sub

    ' cancel if new cell is in protected area

    Private Sub fg_BeforeRowColChange(ByVal OldRow As Long, ByVal OldCol As Long, _

                                      ByVal NewRow As Long, ByVal NewCol As Long, Cancel As Boolean)

        If NewRow >= 2 And NewRow <= 8 And NewCol >= 2 And NewCol <= 4 Then Cancel = True

    End Sub

    ' cancel if new selection is on protected area

    Private Sub fg_BeforeSelChange(ByVal OldRowSel As Long, ByVal OldColSel As Long, _

                                   ByVal NewRowSel As Long, ByVal NewColSel As Long, Cancel As Boolean)

        If (fg.Row < 2 And NewRowSel < 2) Or (fg.Col < 2 And NewColSel < 2) Then Exit Sub

        If (fg.Row > 8 And NewRowSel > 8) Or (fg.Col > 4 And NewColSel > 4) Then Exit Sub

        Cancel = True

    End Sub

See Also

VSFlexGrid Control