3.00 4.00 | 3.00 4.00
WINDOWS | WINDOWS NT
kbprg kbcode kbhowto
The information in this article applies to:
- Standard, Professional, and Enterprise Editions of Microsoft
Visual Basic, 16-bit and 32-bit, for Windows, version 4.0
- Standard and Professional Editions of Microsoft Visual
Basic for Windows, version 3.0
SUMMARY
The Grid custom control does not allow different colors in individual
cells. The example program below demonstrates how you can manipulate color
of individual cells in the Grid custom control.
MORE INFORMATION
The example program shown below enables you to have different background
colors for the cells of a grid. It also shows how to change the text color
in individual cells. It uses the Picture property of the grid and an
invisible picture box control.
Steps to Create Example Program
- Start Visual Basic or on the File menu, click New Project (ALT, F, N)
if Visual Basic is already running. Form1 is created by default.
- On the File menu, click Add File. Then select the GRID.VBX file. The
Grid tool appears in the Toolbox. (In the case of Visual Basic 4.0,
on the Tools menu, click Custom Controls. Then check the Microsoft Grid
Control under the available controls. Finally, click OK).
- Place a grid (Grid1), a picture box (Picture1) and 3 command buttons
(Command1, Command2, Command3) on Form1. Set the Grid1 Cols and Rows
properties both to 10. Then size the grid to show all the cells. Set the
Picture1 AutoRedraw property to True and the Visible property to False.
Make sure that the size of the Picture1 picture box is greater than that
of the cell size of Grid1.
- Place the following functions in the general section of Form1:
' Enter the following on a single line.
Sub SetGridCell (grd As Grid, RowNum%, ColNum%, BkClr&, FrClr&)
grd.Row = RowNum%
grd.Col = ColNum%
Picture1.BackColor = BkClr
Picture1.ForeColor = FrClr
Picture1.CurrentX = 0: Picture1.CurrentY = 0
Picture1.Print grd.Text
grd.Picture = Picture1.Image
End Sub
Sub ClearGrid (grd As Grid)
Dim i%, j%
For i = 1 To grd.Rows - 1
For j = 1 To grd.Cols - 1
grd.Row = i: Grid1.Col = j
grd.Picture = LoadPicture("")
Next j
Next i
End Sub
- Enter the following code in the Form_Load procedure:
Sub Form_Load ()
Dim i%, j%
For i = 1 To Grid1.Rows - 1
For j = 1 To Grid1.Cols - 1
Grid1.Row = i: Grid1.Col = j
' Fill cell text so that "(i,j)" string is
' assigned to the cell in ith row, jth column.
Grid1.Text = "(" & CStr(i) & "," & CStr(j) & ")"
Next j
Next i
End Sub
- Add the following code to the command button click event procedures:
Sub Command1_Click ()
Dim i%, j%
For i = 1 To Grid1.Rows - 1
For j = 1 To Grid1.Cols - 1
Call SetGridCell(Grid1, i, j, QBColor(i-1), QBColor(15))
Next j
Next i
End Sub
Sub Command2_Click ()
Dim i%, j%
For i = 1 To Grid1.Rows - 1
For j = 1 To Grid1.Cols - 1
Call SetGridCell(Grid1, i, j, QBColor(15), QBColor(j-1))
Next j
Next i
End Sub
Sub Command3_Click ()
Call ClearGrid(Grid1)
End Sub
- Press the F5 key to run the program. Click Command1 to see the cells in
different rows appear in different colors. Click Command2 to see text in
cells of different columns appear in different colors. Click Command3 to
reset the grid display.
|