LAB 7.3 - The ListBox
CIS241
Goals:
w
Learn about the ListBox controlw
Learn to use the With statementw
Learn about constantsw
Use the ListBox control to make selectionsw
Read data from file into a listboxw Modify an existing program, applying new techniques
__1. Add a listbox to a project
w Open a new VB project, renaming and saving appropriately
w Using online Help look up ListBox control
v What is the method for placing something into the listbox?
v For removing something from the listbox?
v Selection of an item is done by clicking on it. What property
indicates which item was selected?
v What property indicates how many items are in the listbox?
w Place a listbox, lbxSizes, onto your form.
__2. Learn about the
With statementw Look up With statement in online Help.
v What is its main purpose?
v Look at the example. Do you understand it?
w Copy and paste the code below into the Form_Load procedure,
filling in the missing details.
Private Sub Form_Load()
With lbxSizes
.AddItem "1 x 1"
.AddItem "1 x 2"
.AddItem "1 x 3"
. . . .
.AddItem "5 x 3"
.AddItem "5 x 4"
. AddItem "5 x 5"
End With
w Run the program.
v Notice that if the listbox is too small, a scroll bar will appear.
__3. Learn how
ListIndex works.w Place two labels, lblIndex and lblContents, onto the form.
w Double click on the listbox to bring up the code window.
v Paste this code into the code window.
lblIndex.Caption = lbxSizes.ListIndex
lblContents.Caption = lbxSizes
v Run the program. Click on the listbox.
v What is the smallest value you get? The largest?
v Can you find a relationship between the value of ListIndex and
the column x row displayed in the listbox?
__4. Use the listbox to make selections.
w Place a flexgrid, fgdTest, onto the form.
v Make its FixedRows and FixedCols 0.
w Declare a constant for the cell size.
v Constants are used for values that will not change during
the run of a program, though they may change from one run
to another.
v Paste the code below in the General Declarations section.
Option Explicit
Const cellsize = 500
w Add a procedure, SetGrid, to your program giving it two parameters,
nrow and ncolm.
v Give it the code below.
Dim ro As Integer
Dim colm As Integer
With fgdTest
.Height = nrow * cellsize
.Width = ncolm * cellsize
.Rows = nrow
.Cols = ncolm
For ro = 0 To nrow - 1
.RowHeight(ro) = cellsize
Next ro
For colm = 0 To ncolm - 1
.ColWidth(colm) = cellsize
Next colm
End With
v How does the code set the overall size of the flexgrid?
v How do the individual cells get their size?
v How do we determine the number of cells in the grid?
v Why do we see 0 To nrow - 1in the code?
w Now change the code of lbxSizes_Click as you see below.
Dim numrows As Integer
Dim numcols As Integer
lblIndex.Caption = lbxSizes.ListIndex
lblContents.Caption = lbxSizes
numrows = lbxSizes.ListIndex Mod 5 + 1
numcols = lbxSizes.ListIndex \ 5 + 1
Call SetGrid(numrows, numcols)
w Run the program. What happens?
__5. Are you ready for a challenge?
w The code for lbxSizes_Click is slightly inefficient. We declare two
variables but we use them only to call the procedure SetGrid.
Rewrite the code to eliminate the need for these two variables.
w There is another way to determine the number of rows and columns
selected by the user which does not involve computing those values
from the list index. Can you think of what it is? If so, write code to
test it out.
w Our approach has been to set the cell size and to adjust the grid size
to match the specified number of rows and columns. Instead, set the
grid size and adjust the cell size to match. Here are some hints.
v Make a copy of the project you now have and make changes
to that copy.
v Instead of cellsize, make gridsize the constant and giving it an
appropriate value.
v Use this constant value to set the size of the grid in Form_Load.
v Calculate the values of the cell height and width based on the
grid size and on the number of rows and columns in the grid.
Pass these values into
SetGrid as additional parameters.X What data type did you use for these parameters?
X Why?
v Make appropriate adjustments to SetGrid.
X What changes did you make to SetGrid?
{Notice that the cells will no longer be perfectly square.}