iGrid Control Tour - Code Samples (II)
Editing control
You can use many iGrid events to control the editing process in this grid. Among of them are RequestEdit, BeforeCommitEdit, AfterCommitEdit and CancelEdit.
When the user attempts to edit any cell, the RequestEdit method is fired. You can check some conditons in this event and to decide whether the user will edit the cell. For instance, you can protect the cells in the second column from editing using the following code:
Private Sub iGrid1_RequestEdit(_
ByVal lRow As Long, ByVal lCol As Long, _
ByVal iKeyAscii As Integer, bCancel As Boolean, _
sText As String, lMaxLength As Long)
If lCol = 2 Then
bCancel = True
End If
End Sub
The next event, BeforeCommitEdit, is fired when the user attempts to save the edited cell text. You can prevent a cell in the column with the "sum" key of entering any non-positive numbers with the help of the following lines of code:
Private Sub iGrid1_BeforeCommitEdit(_
ByVal lRow As Long, ByVal lCol As Long, _
eResult As EEditResults, _
ByVal sNewText As String, vNewValue As Variant)
If iGrid1.ColKey(lCol) = "sum" Then
If Val(vNewValue) < 0 Then
MsgBox "Only positive numbers!"
eResult = igEditResProceed
End If
End If
End Sub
The eResult parameter of the BeforeCommitEdit method allows to control how iGrid works while the user attempts to commit cell changes. By default, this parameter has the igEditResCommit value. It means that iGrid will save any entered data in the cell value. If you assign igEditResCancel to this parameter, all user input will be aborted. The last value, igEditResProceed, allows to proceed the editing process without its completion.
Fitting row heights
In iGrid, row height can be set independently for each row, or automatically calculated based on the contents of the cells (autoheighting). To set the minimum height of each row when texts in all cells will be visible, use the following code:
Private Sub mnuFitRowHeights_Click()
Dim lRow As Long
iGrid1.Redraw = False
For lRow = 1 To grdCust.RowCount
iGrid1.AutoHeightRow lRow
Next
iGrid1.Redraw = True
End Sub
The Redraw property allows to avoid needless refreshments whenever you are changing a large number of cells. Thus you can highly speed up your code.
How to ensure the required cell is visible
The iGrid control has many useful methods that lighten your programming work. Among of them is the EnsureVisible method. This method checks whether the specified cell is visible in the viewable part of the grid. If not, this method automatically scrolls the grid so the specified cell is visible. You can be sure that the cell in the "title" column in the third row is visible on the screen using the following statement:
iGrid1.EnsureVisible 3, "title"
How to calculate cell coordinates
You can get any cell coordinates using the CellBoundary method of iGrid. The following code displays the coordinates of the currently selected cell when you are clicking a form:
Private Sub Form_Click()
Dim lLeft As Long, lTop As Long
Dim lWidth As Long, lHeight As Long
iGrid1.CellBoundary iGrid1.CurRow, iGrid1.CurCol, _
lLeft, lTop, lWidth, lHeight
MsgBox "Left: " & lLeft & ", Top: " & lTop & vbCrLf & _
"Width: " & lWidth & ", Height: " & lHeight
End Sub
You can also evaluate cell text height and width using built-in methods and implement many other useful tasks that other grids cannot do.
|