LAB 6.0 - Arrays in Color

Goals:

    w Implement one-dimensional arrays

    w Implement For-Next Loops

    w Use color constants

 

__1. Create and use a one-dimensional array

          w Start a new Visual Basic program.

         

   w Name the form frmColor and save it and the project

         

   w Create the array

     v Type the following in the General Declarations section of

                     code  [Remember:: to get there, double-click on the form,

                     then move the cursor to the top, just under Option Explicit,

                     but above the header for Form_Load].

                                    Dim intray (0 To 4) As Integer

         

   w Give values to the array

     v Open the code window and go to the Form_Load procedure.

     v Give it the following code::

          [Remember:: You can save some typing by copying and pasting].

                                    intray (0) = 5

                                    intray (1) = 15

                                    intray (2) = 25

                                    intray (3) = 35

                                    intray (4) = 45

         

   w Create a label for displaying those values; call it lblValue.

         

   w Create a mechanism for displaying the values

     v Create a control array of five command buttons;

                     call it cmdValue.

     v Make the captions Zero, One, Two, Three and Four.

     v Give it the following code:

                                    lblValue.Caption = intray(index)

         

   w Test it.  Describe what happens.

 

__2. Now add some color to your project.

         

   w Declare another array hue, also in the range 0 to 4, but dim its

              type Long.

         

   w Add to Form_Load the following code::

                        hue(0) = vbBlue

                        hue(1) = vbRed

                        hue(2) = vbGreen

                        hue(3) = vbMagenta

                        hue(4) = 8888

         

   w Change the code for cmdValue to the following::

                                    lblValue.Backcolor = hue(index)

         

   w Run the program.  What happens?

            

   w What color is “8888”?

         

   w Why do you think hue was declared of type Long?

         

__3. Practice using For-Next

 

          w Place another command button cmdShowFor onto the form.

     v Let its caption be show values

     v Give it this code:

                                    Dim i   As Integer

                                      frmColor.Print "Below we show the array indices"

                                      frmColor.Print "Before the loop the value of i is", i

                                      For i = 0 To 4

                                        frmColor.Print i

                                      Next i

                                      frmColor.Print "And the current value of i is", i

     v Make some room on the left side of the form, by moving

                   objects, if necessary.  Run the program.  What happens?

     v What is the value of i before executing the loop?

     v What is its value after executing the loop?


 

          w Now use the For-Next to assign initial values to intray and also to

             quadruple the values in that array.  Begin by altering Form_Load in

             the following manner:

     v Declare i as type Integer as the first line of code of

                   Form_Load.

     v Replace the 5 statements which assign values to intray with

                   the code below:

                                      For i = 0 To 4

                                        intray(i) = 10 * i + 5

                                      Next i

 

          w For the last time replace the code for cmdValue by:

                        lblValue.Caption = intray(index)

 

          w Place two command buttons on the form, cmdRaise and cmdLower.

 

          w Give cmdRaise with the below:

                        Dim i As Integer

                          For i = 0 To 4

                             intray(i) = intray(i) * 4

                         Next i

     v Run the program.  What happens?

     v How many clicks of Raise does it take to produce the error

                    message?  Why?

 

          w Now replace the code of cmdLower with code that cuts each value

             of intray by half.  What code did you write?

 

          w Test your program.  Does it work?  How many clicks of Lower

             does it take to for the values to stabilize?  Why? At what value do

             they stabilize?  Why?  Can Raise raise the values now?

         

__4. Exit the program and close Visual Basic.  Congratulations!  You have

         just finished another lab. 


LAB 6.1 - Error Messages

Goals:

    w Use string arrays

    w Learn about array bounds checking

    w Use message boxes to print error messages

 

__1. Create and use a one-dimensional array

          w Start a new Visual Basic program.

          w Name the form frmPrez

          w Name the project Hey

          w Save the form and the project

 

__2. Practice using string arrays.

          w Create the array

     v Type the following in the General Declarations section of

                      code.

                                    Dim prez (1 To 4) As String

          w Create a label for displaying those values; call it lblValue.

          w Create a mechanism for displaying the values

     v Create a control array of five command buttons;

                     call it cmdValue.

     v Make the captions Zero, One, Two, Three and Four.

 

          w Add to Form_Load the following code.

          [Remember:: You can save some typing by copying and pasting].

                        prez(1) = "Washington"

                        prez(2) = "Adams"

                        prez(3) = "Jefferson"

                        prez(4) = "Madison"

          w Give cmdValue the following code:

                        lblValue.Caption = prez(index)

          w Run the program.  Describe what happens.

          w What happens when you press the Zero button?  What error

              message comes up?  What does it mean?

          w Press the Debug button.  What line is highlighted?  Why?

          w Stop the program, and then run it again.

     v Again, press the Zero button.

     v When the error message comes up, this time do not press

                   Debug.  Instead press F1.  What happens?

     v What do you learn from the information displayed?  Can you

                    now explain why an error occurred?

 

__3. Learn about message boxes.

          w Place another command button cmdNoNo onto the form.

     v Make its caption Don’t click on me!

     v Give it the following code::

                        Dim dummy as Integer

                           dummy = MsgBox(“Can’t you follow instructions?!”, vbExclamation)

     v Now test it out.  What happens?

 

          w Now let’s apply what we have learned to take care of the missing

             array element in prez.

     v Recall that when we were displaying the names of the first

                   four presidents our code for cmdValue was:

                                    lblValue.Caption = prez(index)

     v Think about what needs to be done in order to have a message

                    box come up when button Zero is pressed.  Notice that a test

                    must be made to determine what the value of Index is.  And

                   we take one of two different actions, depending on this value. 

                   That sounds like an If-Then-Else, doesn’t it?

     v Enter this code for cmdValue:

 

            Dim dummy As Integer

             If index = 0 then

             dummy = MsgBox(“... place an appropriate message here...”,VbExclamation)

             Else

               lblValue.Caption = prez(index)

             End If

 

     v Run it.  How does it work?  What message did you choose

                   to display?

         

__4. Exit the program and close Visual Basic.  Congratulations!  You have

       just finished another lab.


LAB 6.2 - Select Case & FlexGrid

Goals:

    w Learn simple form of Select-Case.

    w Understand and use functions

    w Learn some uses of the grid control

 

__1. Use Select-Case

          w Start a new Visual Basic program.

          w Name the form frmLab32

          w Save the form and the project

          w Place a command button, a textbox and two labels onto the form,

             giving them appropriate names.  The command button will be used

             to trigger events and the textbox for inputting an integer.  One of

             the labels will be for displaying information.  Place the other one

             above the textbox, using it to display this message:: “Input an

             integer between 1 and 10.”

          w Now write code for the click event of the command button.

            Select Case Val(_____.Text)

              Case 1: _____.Caption = "The number is one"

              Case 2: _____.Caption = "The number is two"

              Case 3: _____.Caption = "The number is three"

              . . .

              Case 10: _____.Caption = "The number is ten"

              Case Else: _____.Caption = "That is not an integer between 1 and 10!"

            End Select

     v In the first blank place the name of your textbox.

     v In the following blanks place the name of your label.

     v Fill in the cases 4 through 9 in a similar manner as above.

          w Run the program.

     v Does it work for all values in the specified range?

     v What happens if you enter 111 in the textbox?

     v How about 5.5?  Why?

 


__2. Use more complex features of Select-Case.

          w Change the code of the command button to match the code below.

            Select Case Val(_____.Text)

              Case 1: _____.Caption = "The number is one"

              Case 2 To 4: _____.Caption = "The number is between two and four"

              Case 5: _____.Caption = "The number is five"

              Case Is > 5: _____.Caption = "The number is greater than 5"

              Case Else: _____.Caption = "That number is out of range!"

            End Select

          w Run your program.

     v What do you get on an input of 2?

     v 3?  5.5?  -15? 10? 999? 3.3? 4.4?

          w (Optional) Remove the Val and the parentheses.

     v Now try an input of 10.  What happens?

     v What about 200?

     v What explanation can you give for the behavior we see?

 

__3. Write and use a function.

          w From the Tools menu choose Add Procedure...

     v In the Name: insertion windowlet that opens type Factorial.

     v From the Type choices choose Function.

     v Click on OK.

          w In the code window that opens up...

     v Inside the parentheses of the header type:

                                    ByVal number As Integer

        This will be referred to as the parameter list.

     v Following the parentheses of the header type:

                                    As Long

        This will be referred to as the return type.

     v When you are finished the header should look like this:

                                    Public Function Factorial(ByVal number As Integer) As Long

     v For the function body type:

                                    Dim i As Integer

                                    Dim partialval As Long

                                      partialval = 1

                                      For i = 1 To number

                                        partialval = partialval * i

                                      Next i

                                      Factorial = partialval

          w Retool the command button

     v Place a single quote mark in front of every line of code that

                   you presently have for the command button.

                   [Note: This process will be referred to as commenting out].

     v Did the code change color?

     v Either rename your textbox txtInput or else place another

                   textbox onto the form and give it that name.  Similarly, make

                   sure you have a label named lblDisplay.

     v Following that type the following:

                                     lblDisplay.Caption = Factorial(Val(txtInput.Text))

                                     txtInput.Text = ""

                                     txtInput.SetFocus

          w Run the program

     v What is the value of 0! ?  of 5! ?

     v What is the maximum value for which the factorial can be

                    computed?

          w Explore these concepts

     v Parameters can be ByRef or ByVal.  Which of these is the

                   default?  Hint: Look up ByRef for functions in online Help.

     v Can a command button receive focus?  Hint: Look up

                   the SetFocus method in Help.

 

__4. Use multiple functions

          w Add another function to your program called nCr.

     v Within the parentheses of the header type:

                                    ByVal n As Integer, ByVal r As Integer

     v Make the return type of the function Long.

     v Give the function the code below:

                                    nCr = Factorial(n) / (Factorial(r) * Factorial(n - r))

          w Prepare to use this function

     v Add another textbox txtInput2 to your form.

     v Comment out this line of code from the code of your

                   command button:

                                     lblDisplay.Caption = Factorial(Val(txtInput.Text))

     v Replace it with this code:

                        lblDisplay.Caption = nCr(Val(txtInput.Text), Val(txtInput2.Text))

     v And add this code:

                                    txtInput2.Text = ""


                w Test the code. 

     v Be sure to input a larger number in txtInput than the one in

                   txtInput2.

     v What value do you get for the pair 5 & 3?  7 & 4? 11 & 6?

 

__5. Begin the study of the grid control.

          w If you have not already done so, save the previous program.

          w Now start a new program.

     v From the File menu choose New Project.

          w Place a grid onto your form.

     v From the Project menu choose Components....

     v Make sure the Controls tab is chosen.

     v Click in the box next to Microsoft Flexgrid Control 5.0

     v Click on OK.

     v Look at the toolbox.  Do you see any changes?

     v Place a grid fgdGrid onto the form.

    v Make its Cols value 4, Rows 4, FixedCols 0 and FixedRows 0.

    v And make its ScrollBars value 0, so it will not have scroll bars.

          w Resize the grid and its cells

     v Position fgdGrid near the upper left hand corner of the form.

     v In Form_Load the code below to set the height and width

                   of the grid.

                             Dim gridsize as Integer

                                    gridsize = 2000

                                    fgdGrid.Height = gridsize

                                    fgdGrid.Width = gridsize

     v Experiment with different values of gridsize until you have

                   found a size that fits well.  What size did you choose?

     v Still in Form_Load declare i and cellsize as integers.

                   Make sure your declarations precede the statements of the

                   procedure (e.g., the line gridsize = 2000).

     v Now add these line to your code.

                                    cellsize = gridsize / 4

                                    For i = 0 To 3

                                       fgdGrid.RowHeight(i) = cellsize

                                       fgdGrid.ColWidth(i) = cellsize

                                    Next i

     v Run your program.  What happens?

 

__6. Learn how to designate the cells of the grid control.

          w Place two textboxes, txtRow and txtColm, on the form.

          w Give the Click event of fgdGrid this code:

                        txtRow.Text = fgdGrid.Row

                        txtColm.Text = fgdGrid.Col

          w Run the program.  What happens when you click on fgdGrid?

          w Place a command button cmdColorIt onto the form.

     v Give its Click event this code:

                                    fgdGrid.Row = Val(txtRow.Text)

                                    fgdGrid.Col = Val(txtColm.Text)

                                    fgdGrid.CellBackColor = vbBlue

     v Run the program.  What happens when you enter various

                   values in the textboxes, then click on cmdColorIt?

     v Notice that to accomplish this task required three lines of

                     code.  If this were a two-dimensional array we would be able

                     to write just one line code like this:

                                    fgdGrid.(Val(txtRow.Text),Val(txtColm.Text)) = vbBlue

                   Instead, we have to (1) set the row, (2) set the column,

                   and (3) assign the color value to the grid.

          w Suppose we want to turn a cell’s color back to white. 

     v One way to do this is by clicking on it.  Look carefully at the

                   code for fgdGrid_Click.  Notice that the Row and Col

                   properties of fgdGrid designate the cell clicked on.

     v Comment out the code you now have for fgdGrid_Click. 

                   Write code to turn this cell to the color vbWhite.

     v What code did you write?

     v Now test it out to see if it works.

 

__7. And now for a little fun!

          w In the general declarations declare gcColor to be a 2-dimensional

             array of type Integer with indices of 0 to 3 and 0 to 3.  And declare

             a 1-dimensional array hue to be type Long with indices 0 to 6.

          w In Form_Load give hue the values vbWhite, vbRed, vbBlue,

             vbGreen, vbYellow, vbMagenta, and vbCyan.  [If you have forgotten

             how to do this, check Step #2 of Lab 3.0].

          w Now in Form_Load declare ro and colm as variables of type

             Integer.


                w Also in Form_Load initialize gcColor to the value 0 as below:

                        For ro = 0 To 3

                           For colm = 0 To 3

                              gcColor(ro, colm) = 0

                           Next colm

                        Next ro

          w Once again comment out the code your have for fgdGrid_Click.

             Replace it with the code below:

 Dim r As Integer

 Dim c As Integer

 r = fgdGrid.Row

 c = fgdGrid.Col

 Select Case 4 * r + c