Lab 2.1 – CA-2
Goals:
w Make the project multi-form
w Use the Listbox control to
choose CA size
w Allow user to choose
initial configuration by clicking.
__1.
Begin by adapting the project from Lab 2.0.
w Make a new copy of the entire
folder of that project.
v Open the folder that
contains the project folder
(not the project folder
itself).
v Click on the project folder
to highlight it.
v From the Edit menu choose Copy.
v Again from the Edit menu choose Paste. A new folder should
appear, named Copy
of <x>,
where <x> is the name of your
project folder.
v Click on the new folder to
highlight it.
v From the File menu choose Rename. Notice that the name,
Copy
of <x>,
is now highlighted.
v Type in a new name for your
folder.
w Open the new folder.
w Using the same process as
above, make a new copy of the project’s
form file and rename it.
__2.
Make your project multi-form.
w Open the project.
w Add the new copy of the
form file you made in Step1 to your project.
(We will have to trick the VB environment to accomplish our goal).
v Temporarily change the name
of the project’s only form. You can
simply add a letter at the
end of its current name. Do not save
the project or the form at
this point.
v In the toolbar, click on
the Add Form icon (it is the 2nd from the
left).
v Click on the Existing
tab.
v Change the name of the
newly added form to match its external
name. For
example, if in Step1 you named your new form
frmFredNew, that is its external
name. Change the internal name
to match.
v Now change the name of the
old form back to its original.
v And from the File
menu choose Save
Project.
w Why was it necessary to
“trick” VB when adding a second copy of
the cellular automaton form?
w Go through all the code of
the new form and update any references to
the form to make them refer to the new form, instead of the old one.
w Add a “main” form to your
project.
v Again click on the Add
Form icon.
v The New
tab should
already be chosen. Click on Open.
v Rename the form to be frmMain.
v From the File
menu choose Save
Project.
__3.
Implement the multi-form aspect of your project.
w Place labels on the two
matching forms to identify them as they come
up on the screen.
w Place two command buttons
on the main form, with captions Old and
New & Improved.
w Place a third command
button on the main form and make it the Exit
button.
w For each command button
write code to go from the main form to
the appropriate cellular automaton form. For example, if your new
form is named frmFredNew, your code would look like the code
below.
Private
Sub cmdNew_Click()
frmMain.Hide
frmFredNew.Show
End
Sub
w On each of the cellular
automaton forms place a command button
with code to return the program back to the main form. Again, if
your new form is named frmFredNew, your code would look like the
code below.
Private
Sub cmdBack_Click()
frmFredNew.Hide
frmMain.Show
End
Sub
w And now make frmMain
your start up
form.
v From the Project
menu choose <x>
Properties . . .,
where <x> is the
name of your project.
v In the upper right hand of
the dialogue box that pops up find the
little window labeled StartUp
Object:.
v Click on the scroll arrow
to bring up your choices. Choose
frmMain.
v Click on OK.
w Run the project. Verify that you can move freely among all 3
forms.
w If we had not made the
above change, which form would have been
the start up form? Why (i.e.,
which form is the default start up form
in a multi-form project)?
__4.
Now give the user a choice of cellular automaton sizes.
w Add a listbox to the new
cellular automaton form, naming it lbxSizes.
w Add the following code to Form_Load.
For i = 5 To 39
lbxSizes.AddItem Str(i) & " x " & Str(i)
Next i
w Test this code to verify
that the choices come up in the listbox.
w Replace this declaration
Const NumRows = 39
with this declaration
Dim NumRows As Integer
w Make a general procedure, InitGrid. Move code from Form_Load and
from cdmRunIt_Click to produce the following code for InitGrid.
Public
Sub InitGrid()
Dim
cellsize As Integer
Dim
i As Integer
Dim
row As Integer
Dim
col As Integer
fgdCA.Rows = NumRows
fgdCA.Cols = NumRows
cellsize = Round15(Int(Gridsize / NumRows))
fgdCA.Height = cellsize * NumRows + NumRows *
Log(NumRows)
fgdCA.Width = cellsize * NumRows + NumRows *
Log(NumRows)
For i = 0 To NumRows - 1
fgdCA.ColWidth(i) = cellsize
fgdCA.RowHeight(i) = cellsize
Next i
For row = 0 To casize - 1
For col = 0 To casize - 1
old(row, col) = 0
Next col
Next row
old(casize \ 2 - 1, casize \ 2 - 1) = 1
old(casize \ 2, casize \ 2 - 1) = 1
old(casize \ 2 - 1, casize \ 2) = 1
Call DisplayCA
End
Sub
w Notice that this code
incorporates a correction to Lab 2.0 in the
initialization of the array old.
w Add this code to Form_Load.
NumRows = 39
casize = 39
Call InitGrid
w Run the program. Click on command button RunIt
of the new form
to
verify that it still works.
w Place this code under lbxSizes_Click.
NumRows = lbxSizes.ListIndex + 5
casize = NumRows
Call InitGrid
w Run the program. Try it out for various sizes.
w Why was the first line of
code not as below?
NumRows = lbxSizes.ListIndex
__5.
Allow user to select initial configuration.
w Move some code around to
permit a more generic initialization and display.
v Create a general procedure,
StandardConfig.
v Move the 3 lines below from
InitGrid to StandardConfig.
old(casize \ 2 - 1, casize \ 2 - 1) = 1
old(casize \ 2, casize \ 2 - 1) = 1
old(casize \ 2 - 1, casize \ 2) = 1
v Find every place from which
InitGrid is called.
{This should be Form_Load and lbxSizes_Click}.
v Immediately following those
calls to InitGrid, place calls to StandardConfig
and to DisplayCA. When you are finished you should have the following
three lines in place of the
single line calling InitGrid.
Call InitGrid
Call StandardConfig
Call DisplayCA
v Remove from InitGrid the call to DisplayCA.
v Run your program to verify
that it still works the same as before.
w Place another command
button onto the new CA form. Give it
this code.
Call InitGrid
Call DisplayCA
fgdCA.GridLines = 2
w Give fgdCA_Click this code.
Private
Sub fgdCA_Click()
Dim
r As Integer
Dim
c As Integer
r = fgdCA.row
c = fgdCA.col
old(r, c) = (old(r, c) +1) Mod 2
fgdCA.CellBackColor = hue(old(r, c))
End
Sub
w Since you are clicking on a
cell to turn it from white to blue, why do we take
the new value Mod 2? Why not
just add 1 to the old value?
w Run the program. Try out various initial configurations and
various CA sizes.
w Notice that once we turn
the gridlines on, they stay on. Take
steps to enable
the user to turn the gridlines off after choosing the initial
configuration. Try out
your solution.
w How did you solve this problem?