chapter 3 - visual basic schneider chapter 3 fundamentals of programming in visual basic
Post on 19-Dec-2015
234 views
TRANSCRIPT
![Page 1: Chapter 3 - Visual Basic Schneider Chapter 3 Fundamentals of Programming in Visual Basic](https://reader038.vdocuments.site/reader038/viewer/2022102702/56649d385503460f94a12549/html5/thumbnails/1.jpg)
Chapter 3 - Visual Basic Schneider
Chapter 3
Fundamentals of Programming in Visual Basic
![Page 2: Chapter 3 - Visual Basic Schneider Chapter 3 Fundamentals of Programming in Visual Basic](https://reader038.vdocuments.site/reader038/viewer/2022102702/56649d385503460f94a12549/html5/thumbnails/2.jpg)
Chapter 3 - Visual Basic Schneider
Outline and Objective
• Visual Basic Objects• Visual Basic Events
• Numbers• Strings• Input/Output• Built-In Functions
![Page 3: Chapter 3 - Visual Basic Schneider Chapter 3 Fundamentals of Programming in Visual Basic](https://reader038.vdocuments.site/reader038/viewer/2022102702/56649d385503460f94a12549/html5/thumbnails/3.jpg)
Chapter 3 - Visual Basic Schneider
The initial Visual Basic screen
Toolbox
Project Explorerwindow
Properties window
Form
ToolbarMenu bar
![Page 4: Chapter 3 - Visual Basic Schneider Chapter 3 Fundamentals of Programming in Visual Basic](https://reader038.vdocuments.site/reader038/viewer/2022102702/56649d385503460f94a12549/html5/thumbnails/4.jpg)
Chapter 3 - Visual Basic Schneider
Steps to Create a Visual Basic Program
1. Create the Objects
2. Set Properties
3. Write the Code for each Event
![Page 5: Chapter 3 - Visual Basic Schneider Chapter 3 Fundamentals of Programming in Visual Basic](https://reader038.vdocuments.site/reader038/viewer/2022102702/56649d385503460f94a12549/html5/thumbnails/5.jpg)
Chapter 3 - Visual Basic Schneider
Four most useful Visual Basic Controls
• Text Boxes
• Labels
• Command Buttons
• Picture Boxes
![Page 6: Chapter 3 - Visual Basic Schneider Chapter 3 Fundamentals of Programming in Visual Basic](https://reader038.vdocuments.site/reader038/viewer/2022102702/56649d385503460f94a12549/html5/thumbnails/6.jpg)
Chapter 3 - Visual Basic Schneider
A Text Box Walkthrough:
• Double-click on Text Box to add a Text Box to your form
• Activate the Properties window (Press F4)
• Set values of Properties for Text Box
![Page 7: Chapter 3 - Visual Basic Schneider Chapter 3 Fundamentals of Programming in Visual Basic](https://reader038.vdocuments.site/reader038/viewer/2022102702/56649d385503460f94a12549/html5/thumbnails/7.jpg)
Chapter 3 - Visual Basic Schneider
A Text Box Walkthrough
Text box
![Page 8: Chapter 3 - Visual Basic Schneider Chapter 3 Fundamentals of Programming in Visual Basic](https://reader038.vdocuments.site/reader038/viewer/2022102702/56649d385503460f94a12549/html5/thumbnails/8.jpg)
Chapter 3 - Visual Basic Schneider
Some Useful Properties:
• Name
• Caption
• Border style• Visible• Back Color• Alignment• Font
![Page 9: Chapter 3 - Visual Basic Schneider Chapter 3 Fundamentals of Programming in Visual Basic](https://reader038.vdocuments.site/reader038/viewer/2022102702/56649d385503460f94a12549/html5/thumbnails/9.jpg)
Chapter 3 - Visual Basic Schneider
Naming Objects:
• Use the Property window to change the Name property of an object
• Good Programming habit is that each name begins with three letter prefix that identifies the type of control.
![Page 10: Chapter 3 - Visual Basic Schneider Chapter 3 Fundamentals of Programming in Visual Basic](https://reader038.vdocuments.site/reader038/viewer/2022102702/56649d385503460f94a12549/html5/thumbnails/10.jpg)
Chapter 3 - Visual Basic Schneider
Naming Objects:Object Prefix Example
Command Button cmd cmdStart
Form frm frmPayroll
Label lbl lblName
Picture box pic picClouds
Text box txt txtAddress
![Page 11: Chapter 3 - Visual Basic Schneider Chapter 3 Fundamentals of Programming in Visual Basic](https://reader038.vdocuments.site/reader038/viewer/2022102702/56649d385503460f94a12549/html5/thumbnails/11.jpg)
Chapter 3 - Visual Basic Schneider
Visual Basic Events
• Code is a set of statements that will be executed when you run a program.
• Write Code for each Event.
• Most Events are associated with Objects.
• The code for each event is called an “Event Procedure”.
![Page 12: Chapter 3 - Visual Basic Schneider Chapter 3 Fundamentals of Programming in Visual Basic](https://reader038.vdocuments.site/reader038/viewer/2022102702/56649d385503460f94a12549/html5/thumbnails/12.jpg)
Chapter 3 - Visual Basic Schneider
The steps for creating a VB program:
• Create the Interface.
• Set Properties for the objects.
• Write the code that executes when event occur.
![Page 13: Chapter 3 - Visual Basic Schneider Chapter 3 Fundamentals of Programming in Visual Basic](https://reader038.vdocuments.site/reader038/viewer/2022102702/56649d385503460f94a12549/html5/thumbnails/13.jpg)
Chapter 3 - Visual Basic Schneider
An Event Procedure Walkthrough
• Create the interface.
• Set Properties.
• Double click on the object to open the Code window.
• Click on the Procedure box to find the event
• Write the code for that event.
![Page 14: Chapter 3 - Visual Basic Schneider Chapter 3 Fundamentals of Programming in Visual Basic](https://reader038.vdocuments.site/reader038/viewer/2022102702/56649d385503460f94a12549/html5/thumbnails/14.jpg)
Chapter 3 - Visual Basic Schneider
Example of An Event
Private Sub objectName_event ( )
statements
End Sub
Private Sub txtOne_GotFocus( )
txtOne.Font.Size = 12
txtOne.Font.Bold = False
End Sub
![Page 15: Chapter 3 - Visual Basic Schneider Chapter 3 Fundamentals of Programming in Visual Basic](https://reader038.vdocuments.site/reader038/viewer/2022102702/56649d385503460f94a12549/html5/thumbnails/15.jpg)
Chapter 3 - Visual Basic Schneider
More Example
Private Sub cmdButton_Click( )
txtBox.ForeColor = vbRed
txtBox.Font.Size = 24
txtBox.Text = “Hello”
End Sub
![Page 16: Chapter 3 - Visual Basic Schneider Chapter 3 Fundamentals of Programming in Visual Basic](https://reader038.vdocuments.site/reader038/viewer/2022102702/56649d385503460f94a12549/html5/thumbnails/16.jpg)
Chapter 3 - Visual Basic Schneider
Components of Visual BASIC Statements
• Variables
• Keywords (reserved words)
• Constants
![Page 17: Chapter 3 - Visual Basic Schneider Chapter 3 Fundamentals of Programming in Visual Basic](https://reader038.vdocuments.site/reader038/viewer/2022102702/56649d385503460f94a12549/html5/thumbnails/17.jpg)
Chapter 3 - Visual Basic Schneider
Variables
• A storage location in main memory whose value can change during program execution.
• These storage locations can be referred to by their names.
• Every variable has three properties: a Name, a Value, and a Data Type.
• Types of variables: Numeric and String
![Page 18: Chapter 3 - Visual Basic Schneider Chapter 3 Fundamentals of Programming in Visual Basic](https://reader038.vdocuments.site/reader038/viewer/2022102702/56649d385503460f94a12549/html5/thumbnails/18.jpg)
Chapter 3 - Visual Basic Schneider
Rules for Creating Variable Names
• Must begin with a letter.
• Can contain letters, numeric digits.
• Can have up to 255 characters.
• Can Not be restricted keyword.
![Page 19: Chapter 3 - Visual Basic Schneider Chapter 3 Fundamentals of Programming in Visual Basic](https://reader038.vdocuments.site/reader038/viewer/2022102702/56649d385503460f94a12549/html5/thumbnails/19.jpg)
Chapter 3 - Visual Basic Schneider
Numeric Variables
• Used to store Numbers .
• The value is assigned either by the programmer or by calculation.
![Page 20: Chapter 3 - Visual Basic Schneider Chapter 3 Fundamentals of Programming in Visual Basic](https://reader038.vdocuments.site/reader038/viewer/2022102702/56649d385503460f94a12549/html5/thumbnails/20.jpg)
Chapter 3 - Visual Basic Schneider
Valid Numeric Variable Names:
timeElapsed
taxRate
speed
n
celsius
![Page 21: Chapter 3 - Visual Basic Schneider Chapter 3 Fundamentals of Programming in Visual Basic](https://reader038.vdocuments.site/reader038/viewer/2022102702/56649d385503460f94a12549/html5/thumbnails/21.jpg)
Chapter 3 - Visual Basic Schneider
Invalid Numeric Variable Names:
• maximum/average
• 1stChoice
• square yard
![Page 22: Chapter 3 - Visual Basic Schneider Chapter 3 Fundamentals of Programming in Visual Basic](https://reader038.vdocuments.site/reader038/viewer/2022102702/56649d385503460f94a12549/html5/thumbnails/22.jpg)
Chapter 3 - Visual Basic Schneider
Constant
• Similar to a variable, but can NOT change during the execution of a program.
• Types of Constants:– numeric constants– string constants
![Page 23: Chapter 3 - Visual Basic Schneider Chapter 3 Fundamentals of Programming in Visual Basic](https://reader038.vdocuments.site/reader038/viewer/2022102702/56649d385503460f94a12549/html5/thumbnails/23.jpg)
Chapter 3 - Visual Basic Schneider
Valid Numeric Constants:
Integer Real number
-2987 -1900.05
+16 0.0185
5 10.56
![Page 24: Chapter 3 - Visual Basic Schneider Chapter 3 Fundamentals of Programming in Visual Basic](https://reader038.vdocuments.site/reader038/viewer/2022102702/56649d385503460f94a12549/html5/thumbnails/24.jpg)
Chapter 3 - Visual Basic Schneider
Invalid Numeric Constants:
14,005.5 6.8%
33- $190.04
15 78 3.5&
![Page 25: Chapter 3 - Visual Basic Schneider Chapter 3 Fundamentals of Programming in Visual Basic](https://reader038.vdocuments.site/reader038/viewer/2022102702/56649d385503460f94a12549/html5/thumbnails/25.jpg)
Chapter 3 - Visual Basic Schneider
Numeric Constants in a Statement:
tax = 0.02 * (income - 500 * dependence)
sum = 2 + x + 4.6 + y
![Page 26: Chapter 3 - Visual Basic Schneider Chapter 3 Fundamentals of Programming in Visual Basic](https://reader038.vdocuments.site/reader038/viewer/2022102702/56649d385503460f94a12549/html5/thumbnails/26.jpg)
Chapter 3 - Visual Basic Schneider
String Constants:
• A group of alphanumeric data consisting of any type of symbols.
![Page 27: Chapter 3 - Visual Basic Schneider Chapter 3 Fundamentals of Programming in Visual Basic](https://reader038.vdocuments.site/reader038/viewer/2022102702/56649d385503460f94a12549/html5/thumbnails/27.jpg)
Chapter 3 - Visual Basic Schneider
Valid String Constants
“A rose by any other name”
“Down By the Sea Shore”
“134.23”
“She said, ‘stop , thief!’”
![Page 28: Chapter 3 - Visual Basic Schneider Chapter 3 Fundamentals of Programming in Visual Basic](https://reader038.vdocuments.site/reader038/viewer/2022102702/56649d385503460f94a12549/html5/thumbnails/28.jpg)
Chapter 3 - Visual Basic Schneider
Invalid String Constants
‘Down by the Seashore’
“134.24
“She said, “Stop, thief!””
![Page 29: Chapter 3 - Visual Basic Schneider Chapter 3 Fundamentals of Programming in Visual Basic](https://reader038.vdocuments.site/reader038/viewer/2022102702/56649d385503460f94a12549/html5/thumbnails/29.jpg)
Chapter 3 - Visual Basic Schneider
Arithmetic Operations & Hierarchy of Operations
Operator operation Basic expression
^ Exponentiation A ^ B
* Multiplication A * B
/ Division A / B
+ Addition A + B
- Subtraction A - B
![Page 30: Chapter 3 - Visual Basic Schneider Chapter 3 Fundamentals of Programming in Visual Basic](https://reader038.vdocuments.site/reader038/viewer/2022102702/56649d385503460f94a12549/html5/thumbnails/30.jpg)
Chapter 3 - Visual Basic Schneider
Examples
Evaluate the following expressions:
x = 3 * 6 - 12 / 3
x = 4 ^ (8 / 4)
y = 12 + 6 / (3 * (10 - 9))
z = 5 + 4 ^ 2
m = 6 / 3 + 3
![Page 31: Chapter 3 - Visual Basic Schneider Chapter 3 Fundamentals of Programming in Visual Basic](https://reader038.vdocuments.site/reader038/viewer/2022102702/56649d385503460f94a12549/html5/thumbnails/31.jpg)
Chapter 3 - Visual Basic Schneider
Keywords
• Words that have predefined meaning to Visual Basic .
• Can Not be used as variable names.
Example: Print
Cls
If
While
![Page 32: Chapter 3 - Visual Basic Schneider Chapter 3 Fundamentals of Programming in Visual Basic](https://reader038.vdocuments.site/reader038/viewer/2022102702/56649d385503460f94a12549/html5/thumbnails/32.jpg)
Chapter 3 - Visual Basic Schneider
Visual Basic Print Statement
• Print: Is a method used to display data on the screen or printer.
• Can be used to print value of variables.
• Can be used to print value of arithmetic expressions .
![Page 33: Chapter 3 - Visual Basic Schneider Chapter 3 Fundamentals of Programming in Visual Basic](https://reader038.vdocuments.site/reader038/viewer/2022102702/56649d385503460f94a12549/html5/thumbnails/33.jpg)
Chapter 3 - Visual Basic Schneider
Example of Print Statements
Private Sub cmdCompute_Click()
picResults.Print 3 - 2
picResults.Print 3 * 2
picResults.Print 3 / 2
picResults.Print 3 ^ 2
picResults.Print 2 * (3 + 4)
End Sub
![Page 34: Chapter 3 - Visual Basic Schneider Chapter 3 Fundamentals of Programming in Visual Basic](https://reader038.vdocuments.site/reader038/viewer/2022102702/56649d385503460f94a12549/html5/thumbnails/34.jpg)
Chapter 3 - Visual Basic Schneider
Example of Print Statement
• picOutput.Print speed
• picOutput.Print taxRate
• picOutput.Print “Class average is”; total / 3
![Page 35: Chapter 3 - Visual Basic Schneider Chapter 3 Fundamentals of Programming in Visual Basic](https://reader038.vdocuments.site/reader038/viewer/2022102702/56649d385503460f94a12549/html5/thumbnails/35.jpg)
Chapter 3 - Visual Basic Schneider
Example
x = 15
y = 5
picOutput.Print (x + y) / 2, x / y
![Page 36: Chapter 3 - Visual Basic Schneider Chapter 3 Fundamentals of Programming in Visual Basic](https://reader038.vdocuments.site/reader038/viewer/2022102702/56649d385503460f94a12549/html5/thumbnails/36.jpg)
Chapter 3 - Visual Basic Schneider
Output
10 3
![Page 37: Chapter 3 - Visual Basic Schneider Chapter 3 Fundamentals of Programming in Visual Basic](https://reader038.vdocuments.site/reader038/viewer/2022102702/56649d385503460f94a12549/html5/thumbnails/37.jpg)
Chapter 3 - Visual Basic Schneider
Internal Documentation
• An apostrophe (‘) can be used to indicate comments; comments are ignored by Visual Basic.
• The keyword Rem can also be used instead of an apostrophe for comments.
• Remarks can also be placed after program statement too.
![Page 38: Chapter 3 - Visual Basic Schneider Chapter 3 Fundamentals of Programming in Visual Basic](https://reader038.vdocuments.site/reader038/viewer/2022102702/56649d385503460f94a12549/html5/thumbnails/38.jpg)
Chapter 3 - Visual Basic Schneider
Visual Basic Assignment Statement
• The statement var = expr assigns the value of the expression to the variable.
• Assigns the value of the expression on the right to the variable on the left.
![Page 39: Chapter 3 - Visual Basic Schneider Chapter 3 Fundamentals of Programming in Visual Basic](https://reader038.vdocuments.site/reader038/viewer/2022102702/56649d385503460f94a12549/html5/thumbnails/39.jpg)
Chapter 3 - Visual Basic Schneider
Example
Private Sub cmdCompute_Click( )picResults.Cls
a = 5
b = 4
c = a * (2 + b)
picResults.Print c
End Sub
![Page 40: Chapter 3 - Visual Basic Schneider Chapter 3 Fundamentals of Programming in Visual Basic](https://reader038.vdocuments.site/reader038/viewer/2022102702/56649d385503460f94a12549/html5/thumbnails/40.jpg)
Chapter 3 - Visual Basic Schneider
Valid Assignment Statement
count = count + 1
num = 5
count = count + num /2
![Page 41: Chapter 3 - Visual Basic Schneider Chapter 3 Fundamentals of Programming in Visual Basic](https://reader038.vdocuments.site/reader038/viewer/2022102702/56649d385503460f94a12549/html5/thumbnails/41.jpg)
Chapter 3 - Visual Basic Schneider
Invalid Assignments
10 = count
count + 1 = count
![Page 42: Chapter 3 - Visual Basic Schneider Chapter 3 Fundamentals of Programming in Visual Basic](https://reader038.vdocuments.site/reader038/viewer/2022102702/56649d385503460f94a12549/html5/thumbnails/42.jpg)
Chapter 3 - Visual Basic Schneider
String Variables
• A String variable stores character strings.
• The rules for naming string variables are identical to those of numeric variables.
• When a String variable is first declared, its value is the null string. (that is, the empty string).
![Page 43: Chapter 3 - Visual Basic Schneider Chapter 3 Fundamentals of Programming in Visual Basic](https://reader038.vdocuments.site/reader038/viewer/2022102702/56649d385503460f94a12549/html5/thumbnails/43.jpg)
Chapter 3 - Visual Basic Schneider
Example of String Variable
Private Sub cmdShow_Click()
picOutput.Cls
phrase = "win or lose that counts."
picOutput.Print "It's not whether you "; phrase
picOutput.Print "It's whether I "; phrase
End Sub
![Page 44: Chapter 3 - Visual Basic Schneider Chapter 3 Fundamentals of Programming in Visual Basic](https://reader038.vdocuments.site/reader038/viewer/2022102702/56649d385503460f94a12549/html5/thumbnails/44.jpg)
Chapter 3 - Visual Basic Schneider
Concatenation
• Two string can be combined with the concatenation operation.
• Concatenation is represented with the ampersand ( & ) sign.
![Page 45: Chapter 3 - Visual Basic Schneider Chapter 3 Fundamentals of Programming in Visual Basic](https://reader038.vdocuments.site/reader038/viewer/2022102702/56649d385503460f94a12549/html5/thumbnails/45.jpg)
Chapter 3 - Visual Basic Schneider
Example of Concatenation:
strVar1 = “Hello”
strVar2 = “World”
picOutput.Print strVar1& strVar2
![Page 46: Chapter 3 - Visual Basic Schneider Chapter 3 Fundamentals of Programming in Visual Basic](https://reader038.vdocuments.site/reader038/viewer/2022102702/56649d385503460f94a12549/html5/thumbnails/46.jpg)
Chapter 3 - Visual Basic Schneider
Example of Concatenation
txtBox.Text = “32” & CHR(176) & “ Fahrenheit”
![Page 47: Chapter 3 - Visual Basic Schneider Chapter 3 Fundamentals of Programming in Visual Basic](https://reader038.vdocuments.site/reader038/viewer/2022102702/56649d385503460f94a12549/html5/thumbnails/47.jpg)
Chapter 3 - Visual Basic Schneider
Data Types
• Each variable in the program is assigned to a data type.
![Page 48: Chapter 3 - Visual Basic Schneider Chapter 3 Fundamentals of Programming in Visual Basic](https://reader038.vdocuments.site/reader038/viewer/2022102702/56649d385503460f94a12549/html5/thumbnails/48.jpg)
Chapter 3 - Visual Basic Schneider
Declaring Variable Types
• Use the Dim statement to Declare the type of a variable.
Example:
Dim number As Integer
Dim flower As String
Dim interestRate As Single
![Page 49: Chapter 3 - Visual Basic Schneider Chapter 3 Fundamentals of Programming in Visual Basic](https://reader038.vdocuments.site/reader038/viewer/2022102702/56649d385503460f94a12549/html5/thumbnails/49.jpg)
Chapter 3 - Visual Basic Schneider
Data Types :
• Single-precision numeric variable: Stores real numbers
• Double-precision numeric variable: Stores real numbers with many digits
• Integer: Stores integers
• Long integer: Stores integers with many digits
![Page 50: Chapter 3 - Visual Basic Schneider Chapter 3 Fundamentals of Programming in Visual Basic](https://reader038.vdocuments.site/reader038/viewer/2022102702/56649d385503460f94a12549/html5/thumbnails/50.jpg)
Chapter 3 - Visual Basic Schneider
Using Text Boxes for Input/Output
• The contents of a text box are always a string.
• Numbers are also stored in text boxes as strings.
![Page 51: Chapter 3 - Visual Basic Schneider Chapter 3 Fundamentals of Programming in Visual Basic](https://reader038.vdocuments.site/reader038/viewer/2022102702/56649d385503460f94a12549/html5/thumbnails/51.jpg)
Chapter 3 - Visual Basic Schneider
Using Text Boxes for Input/Output
• Therefore, the contents of a text box should be changed to a number before being assigned to a numeric variable.
• Val (txtBox.Text) changes the input string into a number.
Example:
numVar = Val (txtBox.Text)
![Page 52: Chapter 3 - Visual Basic Schneider Chapter 3 Fundamentals of Programming in Visual Basic](https://reader038.vdocuments.site/reader038/viewer/2022102702/56649d385503460f94a12549/html5/thumbnails/52.jpg)
Chapter 3 - Visual Basic Schneider
Example (convert miles to furlong and vice versa)
Private Sub txtFurlong_LostFocus()
txtMile.Text = Str(Val(txtFurlong.Text / 8))
End Sub
Private Sub txtMile_LostFocus()
txtFurlong.Text = Str(8 * Val(txtMile.Text))
End Sub
![Page 53: Chapter 3 - Visual Basic Schneider Chapter 3 Fundamentals of Programming in Visual Basic](https://reader038.vdocuments.site/reader038/viewer/2022102702/56649d385503460f94a12549/html5/thumbnails/53.jpg)
Chapter 3 - Visual Basic Schneider
The KeyPress Event Procedure
Private Sub txtCharacter_KeyPress(KeyAscii As Integer)
txtCharacter.Text = ""
picOutput.Cls
picOutput.Print Chr(KeyAscii); " has ANSI value"; KeyAscii
End Sub
![Page 54: Chapter 3 - Visual Basic Schneider Chapter 3 Fundamentals of Programming in Visual Basic](https://reader038.vdocuments.site/reader038/viewer/2022102702/56649d385503460f94a12549/html5/thumbnails/54.jpg)
Chapter 3 - Visual Basic Schneider
Reading Data from Files
1. Choose a number to be the reference number to the file.
2. Set the mode in which the file is to be used:
– Input
– Output
– Append
3. Read the data sequentially using Input statement.
4. Close the file .
![Page 55: Chapter 3 - Visual Basic Schneider Chapter 3 Fundamentals of Programming in Visual Basic](https://reader038.vdocuments.site/reader038/viewer/2022102702/56649d385503460f94a12549/html5/thumbnails/55.jpg)
Chapter 3 - Visual Basic Schneider
Example of Reading from a File:
Open “DATA.TXT” for Input As #1
Input #1, num1
Input #1, num2
picOutput.Print num1+num2
Close #1
Reference number
Read the data and assign it to num1
Open the file
Read from the file
Close the file
![Page 56: Chapter 3 - Visual Basic Schneider Chapter 3 Fundamentals of Programming in Visual Basic](https://reader038.vdocuments.site/reader038/viewer/2022102702/56649d385503460f94a12549/html5/thumbnails/56.jpg)
Chapter 3 - Visual Basic Schneider
Example of Reading from a File:
Open “Data.txt” for Input As #1
Input #1,num1, num2
picOutput.Print num1+num2
Close #1
![Page 57: Chapter 3 - Visual Basic Schneider Chapter 3 Fundamentals of Programming in Visual Basic](https://reader038.vdocuments.site/reader038/viewer/2022102702/56649d385503460f94a12549/html5/thumbnails/57.jpg)
Chapter 3 - Visual Basic Schneider
Input from an Input Box:
• Use Text Box to obtain input.
• For one piece of input use input box instead of a text box
• Input Box is a predefined dialog box.
![Page 58: Chapter 3 - Visual Basic Schneider Chapter 3 Fundamentals of Programming in Visual Basic](https://reader038.vdocuments.site/reader038/viewer/2022102702/56649d385503460f94a12549/html5/thumbnails/58.jpg)
Chapter 3 - Visual Basic Schneider
Syntax for an Input Box
stringVar = InputBox (prompt, title)
![Page 59: Chapter 3 - Visual Basic Schneider Chapter 3 Fundamentals of Programming in Visual Basic](https://reader038.vdocuments.site/reader038/viewer/2022102702/56649d385503460f94a12549/html5/thumbnails/59.jpg)
Chapter 3 - Visual Basic Schneider
Example of Input Box
Private Sub cmdDisplay_Click()
Dim fileName As String, prompt As String, title As String
Dim houseNumber As Single, street As String
prompt = "Enter the name of the file containing the information."
title = "Name of File"
fileName = InputBox(prompt, title)
Open fileName For Input As #1
Input #1, houseNumber
Input #1, street
picAddress.Print "The White House is at"; houseNumber; street
Close #1
End Sub
After executing an inputbox would pop up
![Page 60: Chapter 3 - Visual Basic Schneider Chapter 3 Fundamentals of Programming in Visual Basic](https://reader038.vdocuments.site/reader038/viewer/2022102702/56649d385503460f94a12549/html5/thumbnails/60.jpg)
Chapter 3 - Visual Basic Schneider
Using Message Box for Output:
• Use message box to get the user’s attention.
• Message box is a predefined dialog box too.
![Page 61: Chapter 3 - Visual Basic Schneider Chapter 3 Fundamentals of Programming in Visual Basic](https://reader038.vdocuments.site/reader038/viewer/2022102702/56649d385503460f94a12549/html5/thumbnails/61.jpg)
Chapter 3 - Visual Basic Schneider
Syntax for Message Box
• MsgBox prompt, , title
![Page 62: Chapter 3 - Visual Basic Schneider Chapter 3 Fundamentals of Programming in Visual Basic](https://reader038.vdocuments.site/reader038/viewer/2022102702/56649d385503460f94a12549/html5/thumbnails/62.jpg)
Chapter 3 - Visual Basic Schneider
Example of Message Box
MsgBox “Nice try, but no cigar”, , “Consolation”
Stays on thescreen until the user presses OK
![Page 63: Chapter 3 - Visual Basic Schneider Chapter 3 Fundamentals of Programming in Visual Basic](https://reader038.vdocuments.site/reader038/viewer/2022102702/56649d385503460f94a12549/html5/thumbnails/63.jpg)
Chapter 3 - Visual Basic Schneider
Formatting the Output:
• Create user friendly output.
• In the Print method, control of the spacing of the output is controlled by the following devices.
![Page 64: Chapter 3 - Visual Basic Schneider Chapter 3 Fundamentals of Programming in Visual Basic](https://reader038.vdocuments.site/reader038/viewer/2022102702/56649d385503460f94a12549/html5/thumbnails/64.jpg)
Chapter 3 - Visual Basic Schneider
Formatting the Output:
• Semicolon
• Comma
• Tab Function
![Page 65: Chapter 3 - Visual Basic Schneider Chapter 3 Fundamentals of Programming in Visual Basic](https://reader038.vdocuments.site/reader038/viewer/2022102702/56649d385503460f94a12549/html5/thumbnails/65.jpg)
Chapter 3 - Visual Basic Schneider
Semicolons
• The next value output is placed in the next column position.
Example:
picOutput.Print “Patrick”; ”Jon”
Output Screen:
PatrickJon
![Page 66: Chapter 3 - Visual Basic Schneider Chapter 3 Fundamentals of Programming in Visual Basic](https://reader038.vdocuments.site/reader038/viewer/2022102702/56649d385503460f94a12549/html5/thumbnails/66.jpg)
Chapter 3 - Visual Basic Schneider
Example of Semicolon
picOutput.Print “Patrick”; ” Jon”
Output Screen:
Patrick Jon
Space here
Space here
![Page 67: Chapter 3 - Visual Basic Schneider Chapter 3 Fundamentals of Programming in Visual Basic](https://reader038.vdocuments.site/reader038/viewer/2022102702/56649d385503460f94a12549/html5/thumbnails/67.jpg)
Chapter 3 - Visual Basic Schneider
Example of Semicolon
picOutput.Print 100; -200; 300
Output Screen:
100 -200 300
One space
Two spaces
![Page 68: Chapter 3 - Visual Basic Schneider Chapter 3 Fundamentals of Programming in Visual Basic](https://reader038.vdocuments.site/reader038/viewer/2022102702/56649d385503460f94a12549/html5/thumbnails/68.jpg)
Chapter 3 - Visual Basic Schneider
Commas
• The next value output is placed in the next available print zone.
![Page 69: Chapter 3 - Visual Basic Schneider Chapter 3 Fundamentals of Programming in Visual Basic](https://reader038.vdocuments.site/reader038/viewer/2022102702/56649d385503460f94a12549/html5/thumbnails/69.jpg)
Chapter 3 - Visual Basic Schneider
Print Zones
• Each print zone is 14 positions wide.
![Page 70: Chapter 3 - Visual Basic Schneider Chapter 3 Fundamentals of Programming in Visual Basic](https://reader038.vdocuments.site/reader038/viewer/2022102702/56649d385503460f94a12549/html5/thumbnails/70.jpg)
Chapter 3 - Visual Basic Schneider
Example of Print Zone
Example:
picOutput.Print “SEE”, ”YOU”, ”SOON”
Output Screen:
SEE YOU SOON
Column 1
Column 15
Column 29
![Page 71: Chapter 3 - Visual Basic Schneider Chapter 3 Fundamentals of Programming in Visual Basic](https://reader038.vdocuments.site/reader038/viewer/2022102702/56649d385503460f94a12549/html5/thumbnails/71.jpg)
Chapter 3 - Visual Basic Schneider
Example of Commas
• A print zone can be skipped by typing consecutive commas
Example:
picOutput.Print “HOURLY”, , “PAY”
Output Screen:
HOURLY PAYColumn 29
![Page 72: Chapter 3 - Visual Basic Schneider Chapter 3 Fundamentals of Programming in Visual Basic](https://reader038.vdocuments.site/reader038/viewer/2022102702/56649d385503460f94a12549/html5/thumbnails/72.jpg)
Chapter 3 - Visual Basic Schneider
Tab Function
• Starts output in the specified column.
• It provides more flexibility in formatting.
• Only use Semicolons with the Tab function.
• Only can be used to advance the print position.
![Page 73: Chapter 3 - Visual Basic Schneider Chapter 3 Fundamentals of Programming in Visual Basic](https://reader038.vdocuments.site/reader038/viewer/2022102702/56649d385503460f94a12549/html5/thumbnails/73.jpg)
Chapter 3 - Visual Basic Schneider
Example of Tab Function
Example: picOutput.Print Tab(3); “Hi there!” ;TAB(25) ;“Bye!”
Output Screen:
Hi there! Bye!
Column 3
Column 25
![Page 74: Chapter 3 - Visual Basic Schneider Chapter 3 Fundamentals of Programming in Visual Basic](https://reader038.vdocuments.site/reader038/viewer/2022102702/56649d385503460f94a12549/html5/thumbnails/74.jpg)
Chapter 3 - Visual Basic Schneider
Example of Tab
Example:
picOutput.Print TAB(25); 5; TAB(15); 4; TAB(5); 3
Output Screen:
5
4
3
Column 25
Column 15
Column 5
![Page 75: Chapter 3 - Visual Basic Schneider Chapter 3 Fundamentals of Programming in Visual Basic](https://reader038.vdocuments.site/reader038/viewer/2022102702/56649d385503460f94a12549/html5/thumbnails/75.jpg)
Chapter 3 - Visual Basic Schneider
Functions:
• What is a function?
• What are advantages of using functions?
• How do you use a function?
![Page 76: Chapter 3 - Visual Basic Schneider Chapter 3 Fundamentals of Programming in Visual Basic](https://reader038.vdocuments.site/reader038/viewer/2022102702/56649d385503460f94a12549/html5/thumbnails/76.jpg)
Chapter 3 - Visual Basic Schneider
What is a function
• A sub program designed to perform a specific task.
• A sub program designed to return a single value to the calling program.
![Page 77: Chapter 3 - Visual Basic Schneider Chapter 3 Fundamentals of Programming in Visual Basic](https://reader038.vdocuments.site/reader038/viewer/2022102702/56649d385503460f94a12549/html5/thumbnails/77.jpg)
Chapter 3 - Visual Basic Schneider
Types of Functions
• Built-In functions (library)
• User-defined functions
![Page 78: Chapter 3 - Visual Basic Schneider Chapter 3 Fundamentals of Programming in Visual Basic](https://reader038.vdocuments.site/reader038/viewer/2022102702/56649d385503460f94a12549/html5/thumbnails/78.jpg)
Chapter 3 - Visual Basic Schneider
Example
• x = Sqr(225)
• y = Int (2.7)
• str1 = Left (“John Smith”, 4)
• number = Rnd
![Page 79: Chapter 3 - Visual Basic Schneider Chapter 3 Fundamentals of Programming in Visual Basic](https://reader038.vdocuments.site/reader038/viewer/2022102702/56649d385503460f94a12549/html5/thumbnails/79.jpg)
Chapter 3 - Visual Basic Schneider
Types of Standard Functions
• Numeric Functions (manipulate numbers)
• String Functions (manipulate strings)
![Page 80: Chapter 3 - Visual Basic Schneider Chapter 3 Fundamentals of Programming in Visual Basic](https://reader038.vdocuments.site/reader038/viewer/2022102702/56649d385503460f94a12549/html5/thumbnails/80.jpg)
Chapter 3 - Visual Basic Schneider
Numeric Functions
Rnd Returns a number between 0 and 1.(excluding 1)
Sqr(n) Returns the square root of a number.
Round(n,r) The number n is rounded to r decimalplaces.
Int(n) Returns the largest integer less thanor equal to a number
![Page 81: Chapter 3 - Visual Basic Schneider Chapter 3 Fundamentals of Programming in Visual Basic](https://reader038.vdocuments.site/reader038/viewer/2022102702/56649d385503460f94a12549/html5/thumbnails/81.jpg)
Chapter 3 - Visual Basic Schneider
Example of Numeric Functions
Private Sub cmdEvaluate_Click()
Dim n As Single, root As Single
picResults.Cls
n = 6.76
root = Sqr(n)
picResults.Print root; Int(n); Round(n,1)
End Sub
![Page 82: Chapter 3 - Visual Basic Schneider Chapter 3 Fundamentals of Programming in Visual Basic](https://reader038.vdocuments.site/reader038/viewer/2022102702/56649d385503460f94a12549/html5/thumbnails/82.jpg)
Chapter 3 - Visual Basic Schneider
Commonly-Used String Functions
Function: Left (“Penguin”,4)
Purpose: Returns the number of specified characters, starting at the beginning of the string.
![Page 83: Chapter 3 - Visual Basic Schneider Chapter 3 Fundamentals of Programming in Visual Basic](https://reader038.vdocuments.site/reader038/viewer/2022102702/56649d385503460f94a12549/html5/thumbnails/83.jpg)
Chapter 3 - Visual Basic Schneider
Commonly-Used String Functions
Function: Right (“Gotham City” , 4)
Purpose: Returns the number of specified characters from the end of the string .
![Page 84: Chapter 3 - Visual Basic Schneider Chapter 3 Fundamentals of Programming in Visual Basic](https://reader038.vdocuments.site/reader038/viewer/2022102702/56649d385503460f94a12549/html5/thumbnails/84.jpg)
Chapter 3 - Visual Basic Schneider
Commonly-Used String Functions
Function: Mid (“Commissioner” , 4, 3)
Purpose: Returns the character string starting at the position indicated by the first number and continuing for the length specified by the second number.
![Page 85: Chapter 3 - Visual Basic Schneider Chapter 3 Fundamentals of Programming in Visual Basic](https://reader038.vdocuments.site/reader038/viewer/2022102702/56649d385503460f94a12549/html5/thumbnails/85.jpg)
Chapter 3 - Visual Basic Schneider
Commonly-Used String Functions
Function: UCase (“Yes”)
Purpose: Converts any lowercase letters in string to uppercase.
![Page 86: Chapter 3 - Visual Basic Schneider Chapter 3 Fundamentals of Programming in Visual Basic](https://reader038.vdocuments.site/reader038/viewer/2022102702/56649d385503460f94a12549/html5/thumbnails/86.jpg)
Chapter 3 - Visual Basic Schneider
String-Related Numeric Functions
Function: InStr (“John Smith”, ” “)
Purpose: Searches for the first occurrence of one string in another and gives the position at which the string is found.
![Page 87: Chapter 3 - Visual Basic Schneider Chapter 3 Fundamentals of Programming in Visual Basic](https://reader038.vdocuments.site/reader038/viewer/2022102702/56649d385503460f94a12549/html5/thumbnails/87.jpg)
Chapter 3 - Visual Basic Schneider
String-Related Numeric Function
Function: Len (“John Smith”)
Purpose: Returns the number of characters in the string.
![Page 88: Chapter 3 - Visual Basic Schneider Chapter 3 Fundamentals of Programming in Visual Basic](https://reader038.vdocuments.site/reader038/viewer/2022102702/56649d385503460f94a12549/html5/thumbnails/88.jpg)
Chapter 3 - Visual Basic Schneider
Format Function
The format functions provide detailed control of how numbers, dates, and strings are displayed.
![Page 89: Chapter 3 - Visual Basic Schneider Chapter 3 Fundamentals of Programming in Visual Basic](https://reader038.vdocuments.site/reader038/viewer/2022102702/56649d385503460f94a12549/html5/thumbnails/89.jpg)
Chapter 3 - Visual Basic Schneider
Examples of Format Functions
FormatNumber (12345.678, 1) 12,345.6
FormatCurrency (12345.678, 2) $12,345.68
FormatPercent (.185, 2) 18.50%
FormatNumber (1 + Sqr(2), 3) 2.414
![Page 90: Chapter 3 - Visual Basic Schneider Chapter 3 Fundamentals of Programming in Visual Basic](https://reader038.vdocuments.site/reader038/viewer/2022102702/56649d385503460f94a12549/html5/thumbnails/90.jpg)
Chapter 3 - Visual Basic Schneider
Format Function
• Format (expr, “@……..@”)
Purpose: The value of this function contains the string right justified in a field of n spaces. Where n is a string of n @ symbols.
![Page 91: Chapter 3 - Visual Basic Schneider Chapter 3 Fundamentals of Programming in Visual Basic](https://reader038.vdocuments.site/reader038/viewer/2022102702/56649d385503460f94a12549/html5/thumbnails/91.jpg)
Chapter 3 - Visual Basic Schneider
Examples:
Format (12345, “@@@@@”) 12345
Format (123, “@@@@@”) 123
Format (“123.4”, “@@@@@”) 123.4
![Page 92: Chapter 3 - Visual Basic Schneider Chapter 3 Fundamentals of Programming in Visual Basic](https://reader038.vdocuments.site/reader038/viewer/2022102702/56649d385503460f94a12549/html5/thumbnails/92.jpg)
Chapter 3 - Visual Basic Schneider
Example
FormatDateTime (“9-15-99”, vbLongDate)
Output: Wednesday, September 15, 1999
![Page 93: Chapter 3 - Visual Basic Schneider Chapter 3 Fundamentals of Programming in Visual Basic](https://reader038.vdocuments.site/reader038/viewer/2022102702/56649d385503460f94a12549/html5/thumbnails/93.jpg)
Chapter 3 - Visual Basic Schneider
Rnd Function
• Returns a random number from 0 up to 1.
(excluding 1).
Example: Displays a random integer from 1 through 6.
picBox.Print Int(6 * Rnd) + 1
![Page 94: Chapter 3 - Visual Basic Schneider Chapter 3 Fundamentals of Programming in Visual Basic](https://reader038.vdocuments.site/reader038/viewer/2022102702/56649d385503460f94a12549/html5/thumbnails/94.jpg)
Chapter 3 - Visual Basic Schneider
Examples of Using Rnd Function:
An integer from 1 through 100?
A number from 2 through 4 (excluding 4)?
An even integer from 2 through 100 ?
Either 0 or 1?