CS105 Lab Discussion 12 Loops - PowerPoint PPT Presentation

1 / 16
About This Presentation
Title:

CS105 Lab Discussion 12 Loops

Description:

CS105 Fall 2006. 4. Coloring a Cell ... Cells(intRow,intCol).Interior.Color = vbBlue. CS105 Fall 2006. 5. Coloring multiple cells ... – PowerPoint PPT presentation

Number of Views:30
Avg rating:3.0/5.0
Slides: 17
Provided by: cs101
Category:
Tags: cs105 | discussion | lab | loops

less

Transcript and Presenter's Notes

Title: CS105 Lab Discussion 12 Loops


1
CS105 Lab Discussion 12Loops
  • Announcements
  • MP5 is posted. Its due Sunday, November 12th at
    1130pm.
  • Midterm 2 feedback sheets will be handed out this
    week in lab.

2
Objectives
  • An introduction to loops.
  • Review module-level variables
  • Download Lab 12 worksheet at http//www.cs.uiuc.e
    du/class/cs105

3
Plan
  • We want to implement a very simple painting
    program that allows brushes of various sizes.
  • Buttons provided
  • Change Brush Size Used to change brush size
  • Paint Used to paint on the given canvas
  • Clear Canvas Clears the canvas

4
Coloring a Cell
  • cmdPaint currently just gets the ActiveCell row
    and column number. Now lets color in the Active
    Cell.
  • We can change the color of a cell by using the
    following notation Cells.Interior.Color
    color
  • This painting program has only one color, Blue.
    To change the color of the cell
  • Cells(intRow,intCol).Interior.Color vbBlue

5
Coloring multiple cells
  • Let's say that the default brush size is 5. Then
    the paint button should color 5 cells in one row
    starting from the Active Cell.
  • Cells(intRow,intCol).Interior.Color vbBlue
  • Cells(intRow,intCol 1).Interior.Color vbBlue
  • Cells(intRow,intCol 2).Interior.Color vbBlue
  • Cells(intRow,intCol 3).Interior.Color vbBlue
  • Cells(intRow,intCol 4).Interior.Color vbBlue
  • This code is redundant, is there a better way to
    do it?

6
For-Loop
  • We can do the same thing using a For-Loop
  • Syntactically, a For-Loop looks like this
  • For intIndex ltstartValuegt To ltendValuegt
  • ltloop bodygt
  • Next intIndex
  • intIndex is the counter that keeps track of how
    many times we have executed the loop.
  • Note intIndex is an integer variable that you
    must declare before the beginning of the loop

7
Flowchart of a For-Next Loop
Initialize counter to start value


Start
Is counter gt end value?
No
Yes
End
8
Code for cmdPaint
  • We rewrite the code using a For-Loop.
  • For intOffset 0 To 4
  • Cells(intRow, intCol intOffset).Interior.Color
    _ vbBlue
  • Next intOffset
  • What does the "Next" statement do?
  • How many cells will be colored blue?

9
Steps in a For-Next Loop
  • If we do not mention the step-size then the
    counter in a For-Next Loop is always incremented
    by 1.
  • What if we want to increase the counter by 2 each
    time?
  • For intOffset 0 To 4 Step 2
  • Try this code!

10
Change Brush Size
  • Currently the brush size is set at 5. We want the
    user to be able to set the brush size.
  • If we do this in cmdPaint, then the user will be
    forced to set the size every time he/she wants to
    paint
  • We will do this in the Brush Size button instead
  • Since the brush size will be used in both
    buttons, we need to define it as a module-level
    variable

11
Module level variables
  • Scoping rules Usually, variables are available
    only within the procedure (or function) they are
    defined in. (local variable)
  • If we want a variable to persist, and be used by
    more than one procedure, then, in VBA, we define
    it outside any one procedure.
  • Naming convention we prefix an m to the
    variable name when its a module level variable.
  • Caution Use module level variables sparingly
    only when asked to on the MPs.

12
Declaring module-level variables
  • Since module-level variables are available to all
    subprocedures, we will declare these variables at
    the top of all the code, right after Option
    Explicit.
  • Dim mintBrushSize As Integer

13
Code for cmdBrush
  • Look at the code for cmdBrush.
  • Currently it gets the brush size from the user
    and checks that it is a number. Now we need to
    store it in mintBrushSize
  • mintBrushSize vntBrushSize

14
Brush Size
  • Now we need to change cmdPaint so it doesn't
    always color the same number of cells
  • Change the for-loop to
  • For intIndex 0 to mintBrushSize - 1
  • Why is the end value mintBrushSize 1 ?

15
Module-level variables
  • This works because the variable mintBrushSize is
    a module-level variable. If we had declared the
    variable inside cmdBrush_Click(), it would not
    work. Why?

16
Some More Things to Try
  • Make the brush draw vertically instead of
    horizontally.
  • Make the brush draw diagonally down and right.
  • Make the brush draw from right to left.
  • Make the brush stroke two cells wide.
Write a Comment
User Comments (0)
About PowerShow.com