You are on page 1of 8

Slide 1 ___________________________________

IT 236 User Interface Development


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

___________________________________

___________________________________

___________________________________

___________________________________

Slide 2 ___________________________________
Tonight:
___________________________________
 (Lab 1 comments…)
 List boxes
 Variables ___________________________________
 Working with numbers
 Working with strings
 Input and output ___________________________________

___________________________________

___________________________________

___________________________________

Slide 3 ___________________________________
List Box Control
___________________________________
 A list box can contain several lines of output
 A scroll bar will appear if needed
 Collection of strings that are displayed is ___________________________________
determined by the Items property
 Naming convention: nameOfListBox
___________________________________

___________________________________

___________________________________

___________________________________
Slide 4 ___________________________________
List Box Operations
___________________________________
 Add a string to the end of the list:
 nameOfListBox.Items.Add(newString)
 Clear contents: ___________________________________
 nameOfListBox.Items.Clear()
 To return/remove specific items:
nameOfListBox.Items(n)

 nameOfListBox.Items.RemoveAt(n)
___________________________________

___________________________________

___________________________________

___________________________________

Slide 5 ___________________________________
Variables
___________________________________
 A variable is a named memory location that
stores a value of a particular data type
 Whole numbers: Integer
 also Byte, Short, Long
___________________________________
 Floating-point numbers: Double
 also Single, Decimal
 Character strings: String ___________________________________

___________________________________

___________________________________

___________________________________

Slide 6 ___________________________________
Declaring and Assigning Values
___________________________________
 Dim variableName As Type
 will assign a default value of 0 (or empty string)

 Dim variableName As Type = value


___________________________________
 will assign a different initial value

 variableName = value ___________________________________


 will assign a new value to the variable

___________________________________

___________________________________

___________________________________
Slide 7 ___________________________________
Naming Rules and Conventions
___________________________________
 A variable name must consist of only letters,
numbers, and underscores ( _ )
 A variable name cannot begin with a number ___________________________________
 Variable names are not case-sensitive
 Conventions are to use “lower camel case”
(please do) and to end each variable name
with the data type (don’t bother…) ___________________________________

___________________________________

___________________________________

___________________________________

Slide 8 ___________________________________
Numerical Functions
___________________________________
 Int(x): returns the largest integer that is less
than or equal to x
 Math.Round(x, n): returns the value of x
rounded to n decimal places (if n is omitted,
___________________________________
rounds x to the nearest integer)
 Math.Sqrt(x): returns the square root of x, as
long as x is negative ___________________________________

___________________________________

___________________________________

___________________________________

Slide 9 ___________________________________
Arithmetic Operations
___________________________________
+ addition
- subtraction
* multiplication ___________________________________
/ division
\ integer division
^ exponentiation ___________________________________
mod remainder

___________________________________

___________________________________

___________________________________
Slide 10 ___________________________________
Options Explicit and Strict
___________________________________
 Option Explicit:
 Each variable must be declared before it is used
 On by default (Leave it alone…)
___________________________________
 Option Strict
 Each variable must have its data type specified
when it is declared
 Off by default (Always turn it on!) ___________________________________

___________________________________

___________________________________

___________________________________

Slide 11 ___________________________________
Operator Precedence Rules
___________________________________
1. Any expression in parentheses
2. Exponentiation (^)
3. Multiplication and division (*, /) ___________________________________
4. Integer division (\)
5. Remainder (mod)
6. Addition and subtraction (+ , -) ___________________________________

___________________________________

___________________________________

___________________________________

Slide 12 ___________________________________
Scope of Variables
___________________________________
 local variables (use whenever possible):
 declared within (at the top of) of an event procedure, and
only exist while it is running


only visible to that particular event procedure
declare with Dim
___________________________________
 form-level variables (use only when needed):
 declared outside of (before) all event procedures, and


exist for the entire duration of the program
visible to all event procedures ___________________________________
 declare with Private (or Public…)

___________________________________

___________________________________

___________________________________
Slide 13 ___________________________________
Converting Strings to Numbers
___________________________________
 Integer.Parse(string) takes a character string
as input, returns the equivalent Integer
 Double.Parse(string) takes a character string
as input, reutrns the equivalent Double ___________________________________
 (There is a Parse function for each numerical
data type…)
 These functions are the opposite of
number.ToString(), which returns a string
___________________________________

___________________________________

___________________________________

___________________________________

Slide 14 ___________________________________
Exceptions
___________________________________
 If you try to parse a string that is not in the
right format, an exception will be generated

Try ___________________________________
…code that may generate an exception…
Catch [variableName As ExceptionType]
…code to be jumped to and executed instead

End Try
if an exception is generated… ___________________________________

___________________________________

___________________________________

___________________________________

Slide 15 ___________________________________
Constants
___________________________________
 To declare a variable whose value cannot be
changed, use the Const keyword:
Const CONSTANT_NAME As Type = value
___________________________________
 An attempt to change the value of a constant
will generate a syntax error
 Naming convention is to use all capital
letters, and underscores between words
___________________________________

___________________________________

___________________________________

___________________________________
Slide 16 ___________________________________
String Properties (optional)
___________________________________
 Strings are also objects, with properties
 For a String word:


word.Length: number of characters in the string
word.ToUpper: result of converting to all upper-
___________________________________
case characters
 word.ToLower: result of converting to all lower-
case characters
 word.Trim: result of removing all leading and ___________________________________
trailing spaces

___________________________________

___________________________________

___________________________________

Slide 17 ___________________________________
String Methods (optional)
___________________________________
 String objects also have methods
 For a String word:
 word.Substring(m, n): returns the substring of
word consisting of n characters beginning with ___________________________________
position m
 word.IndexOf(target): returns the position of the
first occurrence of target within word (-1 if there
are no occurrences) ___________________________________

___________________________________

___________________________________

___________________________________

Slide 18 ___________________________________
Input Boxes
___________________________________
 To prompt the user with a dialog box:
name = inputBox(promptString, titleString)

1. Dialog box will appear with titleString at the ___________________________________


top, and promptString above a text box
2. Box will remain until user types in the text
box and clicks OK
3. String the user typed will be stored in name
___________________________________

___________________________________

___________________________________

___________________________________
Slide 19 ___________________________________
Message Boxes
___________________________________
 To display information in a dialog box:
MessageBox.Show(displayString, titleString)

1. Dialog box will appear with titleString at the ___________________________________


top, and displayString in the box
2. Box will remain until user clicks OK
(Note: Form1_Load event procedures runs just once, when ___________________________________
the form is first displayed…)

___________________________________

___________________________________

___________________________________

Slide 20 ___________________________________
Formatting Output
___________________________________
 To format numerical output, add string
arguments to the ToString() function:
“C” for currency: dollar sign, commas,
___________________________________

parentheses around negative values
 “N” for number: commas, negative sign
 “P” for percentage: multiplies by 100, adds “ %”,
negative sign
 Default is two decimal places, this can be ___________________________________
changed by adding a number after the letter

___________________________________

___________________________________

___________________________________

Slide 21 ___________________________________
Summarizing Example
___________________________________
 Budget worksheet:
 Enter amounts for rent, food, utilities,
entertainment
 Compute and display total monthly expenses ___________________________________
 Compute and display percentage of the budget
dedicated to each category
Format output as currency or percentage as
___________________________________

appropriate

___________________________________

___________________________________

___________________________________
Slide 22 ___________________________________
Next Time:
___________________________________
 Boolean expressions
 Branching
 Lab Session 2 ___________________________________

___________________________________
(Assignment 3 has been posted, due next week…)

___________________________________

___________________________________

___________________________________

You might also like