Sie sind auf Seite 1von 8

Slide 1 ___________________________________

IT 236 User Interface Development ___________________________________


Lecture 6

Eric J. Schwabe
___________________________________
School of CTI, DePaul University
Spring 2008 ___________________________________
___________________________________
___________________________________
___________________________________

Slide 2 ___________________________________
Tonight:
___________________________________
 Go Over Midterm Exam
 List Boxes and Combo Boxes
___________________________________
 Loops
 Do Loops ___________________________________
 For Loops
 Program Construction ___________________________________
 Functions?
___________________________________
___________________________________

Slide 3 ___________________________________
List Box Properties
___________________________________
 A list box displays the strings contained in
the Items collection ___________________________________
 nameOfListBox.Items.Count: number of strings


in the collection
nameOfListBox.SelectedIndex: position of the
___________________________________
currently selected string (between 0 and


Items.Count, or -1 if none selected)
nameOfListBox.Items(i): allows access to string ___________________________________
in position i (generates exception if i is not
between 0 and Items.Count-1)
___________________________________
___________________________________
Slide 4 ___________________________________
Adding Strings to List Box
___________________________________
 nameOfListBox.Items.Add(string): Adds
string to the end of the list of items
___________________________________
 nameOfListBox.Items.Insert(i, string): Inserts
string at position i in the list of items ___________________________________
 0 indicates the beginning of the list


Items.Count indicates the end of the list
generates an exception if i is not between 0 and
___________________________________
Items.Count
___________________________________
___________________________________

Slide 5 ___________________________________
Removing Strings from List Box
___________________________________
 nameOfListBox.Items.RemoveAt(i): removes
the string at position i from the list of items
___________________________________
 Generates exception if i is not between 0 and
Items.Count – 1 ___________________________________
 nameOfListBox.Items.Remove(string):
removes the first occurrence of string from
the list of items ___________________________________
 If it does not appear in the list, nothing happens
___________________________________
___________________________________

Slide 6 ___________________________________
Combo Box
___________________________________
 Similar to a list box, but also includes a text
box where the user can type an input string ___________________________________
 If user types in text box, SelectedValue will
be -1 ___________________________________
 Value in text box can be accessed as
nameOfComboBox.Text
 Three styles: simple (displayed), drop-down ___________________________________
(hidden), drop-down list (hidden, no text box)
___________________________________
___________________________________
Slide 7 ___________________________________
Looping
___________________________________
 Looping statements let us execute an action
(the loop body) repeatedly; the number of
___________________________________
times it is repeated depends on the results of
some repeated test: ___________________________________
 Do Loop (do-while and do-until)
 For Loop
___________________________________
___________________________________
___________________________________

Slide 8 ___________________________________
Do Loop (do-while)
___________________________________
 Repeats an action for as long as some condition is
satisfied; stops when it is no longer the case ___________________________________
 Syntax: Do While test

Loop
action
___________________________________
 When the statement is reached:
1. Evaluate test
2. If result is true, the action; if false, end loop
___________________________________
3. Repeat 1. and 2. until the loop ends

___________________________________
___________________________________

Slide 9 ___________________________________
Do Loop (do-until)
___________________________________
 Repeats an action for as long as some condition is
not satisfied; stops when it becomes the case ___________________________________
 Syntax: Do
action
Loop Until test ___________________________________
 When the statement is reached:
1. Execute the action
2. Evaluate test
___________________________________
3. If result is false, execute the action; if true, end loop
4. Repeat 2. and 3. until the loop ends
___________________________________
___________________________________
Slide 10 ___________________________________
Infinite Loops
___________________________________
 A loop will never end if its test is always true
(do-while) or always false (do-until); this is ___________________________________
called an infinite loop
 To break out of an infinite loop, close the
form
___________________________________
 Be sure that the that loop body modifies
some variable that is involved in the test (not ___________________________________
foolproof, but will avoid some problems…)

___________________________________
___________________________________

Slide 11 ___________________________________
For Loop
___________________________________
 Used when the iterations of the loop can be
“counted” using some variable
___________________________________
 Syntax: For variable = start to finish
Next
action
___________________________________
 start: initial value of variable
 finish: final value of variable ___________________________________
 action: statements that will be executed for each
value in the range from start to finish
___________________________________
___________________________________

Slide 12 ___________________________________
For Loop
___________________________________
 When the statement is reached:
1. Set the value of variable to start
___________________________________
2. Evaluate the test variable <= finish
if it is true, execute action
if it is false, end the loop
___________________________________
3. Set variable equal to variable + 1
4. Repeat 2. and 3. until the loop ends ___________________________________
___________________________________
___________________________________
Slide 13 ___________________________________
For loops vs. Do loops
___________________________________
 For: counter variable
updated automatically
 Do: loop body must
update counter variable ___________________________________
For variable = start

action
to finish
variable = start
Do While variable<=finish
action
___________________________________
Next variable = variable+1
Loop
___________________________________
___________________________________
___________________________________

Slide 14 ___________________________________
General For Loop ___________________________________
 Syntax: For variable = start to finish Step s

Next
action ___________________________________
1. Set the value of variable to start
2. Evaluate test variable <= finish (for positive s), ___________________________________
or variable >= finish (for negative s)
if it is true, execute action
if it is false, end the loop ___________________________________
3. Set variable equal to variable + s
4. Repeat 2. and 3. until the loop ends ___________________________________
___________________________________

Slide 15 ___________________________________
Program Construction
___________________________________
1. Get a clear description of the requirements
2. Develop algorithms for event procedures
___________________________________
3. Write code to implement algorithms
4. Test and debug the program
___________________________________
(Return to earlier steps as necessary…) ___________________________________
___________________________________
___________________________________
Slide 16 ___________________________________
Letter Counting Problem
___________________________________
 Problem: “Count the numbers of each vowel, and
the total number of consonants, in a user’s input ___________________________________
strings.”
 Refined: “When the user clicks on a button, prompt
them for a string using an input box. Compute the ___________________________________
number of occurrences of each vowel (A/a, E/e, I/i,
O/o, U/u) and the total number of consonants in the
input string, and report the string and these six values ___________________________________
on two lines of a list box. Repeat this process until
the user enters an empty string as input.”
___________________________________
___________________________________

Slide 17 ___________________________________
Letter Counting Algorithm
___________________________________
___________________________________
Repeat the following:
Prompt user for an input string
For each character in the input string:
If character is A/a, E/e, I/i, O/o, U/u, increment the
appropriate vowel counter ___________________________________
If character is any consonant, increment the consonant
counter
Display input string in list box ___________________________________
Display six counts in list box

___________________________________
Repeat until user enters the empty string

___________________________________

Slide 18 ___________________________________
Incremental Development
___________________________________
 Outside-in:  Inside-out:
___________________________________
___________________________________
___________________________________
___________________________________
___________________________________
Slide 19 ___________________________________
Functions
___________________________________
 A function is a piece of code that is called
with some input value(s) and returns a single ___________________________________
output value
 Example (to find the ceiling of a number):
Function Ceiling (ByVal x As Double) As Int
___________________________________
Dim result As Double

___________________________________
result = CInt(-Int(-x))
Return result
End Function

 Ceiling(5.4) will return a result of 6


___________________________________
___________________________________

Slide 20 ___________________________________
Function Definition Syntax
___________________________________
Function FunctionName(ByVal varName As _
VarType , ...) As ReturnType
...body of function...
___________________________________
Return expression

___________________________________
End Function
 Function is called with FunctionName(input),
where input is of type VarType
 Return value will be the result of evaluating
expression, and will be of type ReturnType,
___________________________________
___________________________________
___________________________________

Slide 21 ___________________________________
When a function is called…
___________________________________
1. The arguments to the function are evaluated and
the values sent to the function ___________________________________
2. The values of the arguments are assigned to the
corresponding parameters
3. The body of the function is executed
___________________________________
4. Upon reaching a return statement, the return
expression is evaluated ___________________________________
5. The return value is sent back to the calling
procedure and replaces the function call
___________________________________
___________________________________
Slide 22 ___________________________________
Pizza Prices
___________________________________
 Which is the best deal?
6 inch, $2.15
___________________________________
8 inch, $3.95
 12 inch, $7.85

 16 inch, $10.50
___________________________________
 18 inch, $12.65

 Write a function to compute the cost per ___________________________________


square inch
 cost / area = cost / (diameter/2)^2 * 3.14159
___________________________________
___________________________________

Slide 23 ___________________________________
Next Time:
___________________________________
 Functions?
 Sub Procedures?
___________________________________
 Multiform projects?
___________________________________
___________________________________
(Assignment 5 has been posted, due 5/19…)

___________________________________
___________________________________

Das könnte Ihnen auch gefallen