Lab 7.0 – Connect 4 Prep
Goals:
w Learn FlexGrid
basics
w Use VB Colors
w Identify chosen
cell
__0. Change your IDE to
require variable declaration
w After opening VB, but before starting any work,
choose Options
. . .
from the Tools menu.
w Choose the editor tab,
if not already chosen.
w Check the box for “Require Variable Declaration”
w Click on OK.
w Double click on the form to open the code window.
w If you do not find Option Explicit at the top of the window, move
the cursor there
and type them in.
__1. Start a new Visual Basic
program.
w Name the form frmGridTest
w Name the project prjGridTest
w Save the form and then the project in folder Lab7-0
__2. Place a FlexGrid on the
form.
w From the Project menu
choose Components
w Choose the Controls tab,
if not already chosen.
w Put a checkmark in the box for Microsoft FlexGrid Control
6.0
w Click OK
w A new yellow icon should now be in your toolbox in
the last row
w Double click on it to place a grid onto your form
__3. Customize the grid for
our purposes.
w Name the grid fgdTest
w Give the grid these properties:
v Rows and Cols => 5
v FixedRows and Fixed Cols => 0
v ScrollBars => 0
w Declare a constant GridSize and
give it a value in the 1000s.
v In the code
window, immediately following Option Explicit
type Const GridSize = 2000.
v In the Form_Load procedure give fgdTest a Height and a
Width of GridSize.
v Run your
program. If you do not like the size of
the grid,
change
the value of the constant GridSize.
w Use Gridsize to set the size
of the grid itself.
v In Form_Load set the Height and
the Width properties of fgdTest.
w Use Gridsize to size the
cells of the grid.
v In Form_Load declare a local variable cellsize.
v Write code to
give cellsize the value Gridsize/5.
v Write a For-Next loop to set the RowHeight and
the ColWidth
properties of each column and row.
Sample code:
fgdTest.RowHeight(i) = cellsize
w Run the program
v Notice that in
the last row and column the cells are undersized.
v Can you think of
a way to correct this using the Gridlinewidth
and the TwipsPerPixel properties?
__4. Set up a grid of colors.
w Declare a 5-element array hue of type Long.
w Assign the following values to that array:
hue(0) = vbWhite
hue(1) = vbBlue
hue(2) = vbRed
hue(3) = vbGreen
hue(4) = vbYellow
w Where should this code go?
__5. Set up an array to keep
track of what is displayed by the grid.
w Declare a 5x5 array cellhue
of type Integer.
w Give every element in this array a value of 0.
__6. Write code to change the
color of a cell when it is clicked on.
w For Click event of fgdTest, write this code:
Dim r As Integer
r = fgdTest.Row
c = fgdTest.Col
cellhue(r, c) = (cellhue(r, c) + 1) Mod 5
fgdTest.CellBackColor = hue(cellhue(r, c))
w Run the program.
Explain what is happening.
w Now add to the code to display not only the color but
also its
number in the cell clicked on.
w Choose an appropriate font and size for that display.
__7. Write code to place a
mark in the rightmost cell of a row.
w Declare a 1-d array whichcol
of 5 elements.
w Initialize all elements to 5.
w Place a label lblWhat on
the form where it will not be covered by
the grid.
w Comment out the code for the Click event of fgdTest.
w Replace that code with the following:
lblWhat.Caption = ""
r = fgdTest.Row
If whichcol(r) > 0 Then
whichcol(r) = whichcol(r) - 1
fgdTest.Col =
whichcol(r)
fgdTest.CellBackColor = vbBlue
fgdTest.Text = "X"
Else
lblWhat.Caption = "Sorry - Full"
End If
w Run the program, clicking on any cell.
w Can you fill in every cell of any given row?
w Explain how the code works.