week08_vb_final.pdf
TRANSCRIPT
-
8/14/2019 week08_VB_final.pdf
1/58
CC111INTRODUCTION TOCOMPUTERS
VISUAL BASIC
Computer Engineering DepartmentAdopted from AAST Alex Branch
-
8/14/2019 week08_VB_final.pdf
2/58
Intended Learning Objectives
Familiar with the Generation of computerLanguages.
High level of languages translationFamiliar with the Visual Basic Environment.Able to build a simple Visual BasicApplication.
-
8/14/2019 week08_VB_final.pdf
3/58
What is Visual Basic?
High Level4th Generation ProgrammingLanguage
Object and Event DrivenVisual - Windows Based
Integrated Development Environment ( IDE)
-
8/14/2019 week08_VB_final.pdf
4/58
What is VB Used For?
Most Popular Programming Language
Stand alone programs
Customized specialized ApplicationsE- Commerce
Web based shopping forms and inquiries
User friendly Interface to applications
-
8/14/2019 week08_VB_final.pdf
5/58
Used by a computer equipment retail outletto record data regarding customer orders.
Example Order Entry Screen
Allows user to use a mouse to click on boxes for text entry.
Also enables user to click on buttons to initiateprocessing steps.
-
8/14/2019 week08_VB_final.pdf
6/58
Generations of Computer Languages
1st - Machine language - 0110 0011 10002nd - Procedure-oriented languages
FORTRAN -1954 for scientists and engineersCOBOL - 1959 for business applicationsC - 1972 - for UNIX operating systems
3rd - Object-oriented languages4th - Event-driven languages. Example: VB
5th - Natural languages
-
8/14/2019 week08_VB_final.pdf
7/58
Compiler Vs Interpreter
Higher Languages are translated toMachine Language by:
Interpreter Translates instructions to machine code line-by-line.BASIC, Quick Basic,Visual Basic
Compiler Translates the entire program to machine codebefore running it.Fortran, C, C++, Visual Basic is also a compiler
-
8/14/2019 week08_VB_final.pdf
8/58
What are the Objects in VB?
Pre-programmed Code for:Command Buttons
LabelsPicturesText Boxes
Have both data and procedureswrapped together
-
8/14/2019 week08_VB_final.pdf
9/58
What is the Integrated DevelopmentEnvironment (IDE)?
-
8/14/2019 week08_VB_final.pdf
10/58
Visual Basic Environment
GUI (Graphical User Interface) - forms andwindows that the user sees
Property - a characteristic or attribute ofan object such as color and size
Event - a user action such as clicking abutton
Code Editor window - editor speciallydesigned to help you when writing code
-
8/14/2019 week08_VB_final.pdf
11/58
The Form Designer Window show the GUI applicationThe Toolbox is used to drag and drop components to your formsThe Properties window show the value od of the properties of each selectedcomponent(Object)
Visual Studio 2008 IDE Environment
-
8/14/2019 week08_VB_final.pdf
12/58
Visual Basic Environment
Components of the VB design environment (Form view)
-
8/14/2019 week08_VB_final.pdf
13/58
Visual Basic Environment
Components of the VB design environment (Code view)
-
8/14/2019 week08_VB_final.pdf
14/58
The Code Editor Window
Helpwindow
Error message box
Syntax error in Red
-
8/14/2019 week08_VB_final.pdf
15/58
Properties Window
Used to set how a control looksand behaves
Holds its default values
-
8/14/2019 week08_VB_final.pdf
16/58
Control Toolbox
Pointer
Label
Group Frame
Check Box
Combo Box
Horizontal Scroll Bar
Timer
Directory List Box
Shape
Image
OLE
Picture
Text Box
Command Button
Option/Radio Button
List Box
Vertical Scroll Bar
Drive List Box
File List BoxLine
Data Control
-
8/14/2019 week08_VB_final.pdf
17/58
Building Your First Application
There are three primary steps in building a VisualProject:
Place (or draw) controls on the form.Assignproperties to the controls.Write event procedures for the controls.
WelcomeApplication.exe
http://localhost/var/www/apps/conversion/tmp/scratch_1/WelcomeApplication.exehttp://localhost/var/www/apps/conversion/tmp/scratch_1/WelcomeApplication.exe -
8/14/2019 week08_VB_final.pdf
18/58
Starting Visual Basic
S i Vi l S di 2008
-
8/14/2019 week08_VB_final.pdf
19/58
Starting Visual Studio 2008
-
8/14/2019 week08_VB_final.pdf
20/58
Creating New Project
-
8/14/2019 week08_VB_final.pdf
21/58
Project initial interface
-
8/14/2019 week08_VB_final.pdf
22/58
Step1 :Draw your controls
-
8/14/2019 week08_VB_final.pdf
23/58
Step 2 : Assign Properties
-
8/14/2019 week08_VB_final.pdf
24/58
Changing the Form Title
-
8/14/2019 week08_VB_final.pdf
25/58
Changing the Form Title
Edit I P ti
-
8/14/2019 week08_VB_final.pdf
26/58
Edit Image Properties
-
8/14/2019 week08_VB_final.pdf
27/58
Browse for an Image
-
8/14/2019 week08_VB_final.pdf
28/58
e ze o e proper y o ePictureBox
-
8/14/2019 week08_VB_final.pdf
29/58
Edit the Text Property of Label1
The Text Property is changed to
-
8/14/2019 week08_VB_final.pdf
30/58
Accessing components properties from code
Label1
PictureBox1
Button1
Each Component has a name, you access its property by:componentName.PropertyName
-
8/14/2019 week08_VB_final.pdf
31/58
Step 3:Writing the Code (Events)
Double click on the Button then write your code
-
8/14/2019 week08_VB_final.pdf
32/58
Writing the Code (Events)
-
8/14/2019 week08_VB_final.pdf
33/58
Running the Application
-
8/14/2019 week08_VB_final.pdf
34/58
The Sub Statement
WherePrivate is the default procedure typeSub indicates beginning of procedurecontrolname is name of associated control
_ (underscore) required separatoreventname is name of corresponding event
( ) set of parentheses is requiredEnd Sub indicates end of a procedure
Private Sub ControlName_eventName(ByVal sender As System.Object, ByVal e AsSystem.EventArgs) Handles ControlName.eventName`
End Sub
Private Sub Button1_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles Button1.Click
Const pi = 3.14Dim r As IntegerDim area As Doubler = Val(TextBox1.Text)area = pi * r * rLabel3.Text = Str(area)
End Sub
Example on the Sub Statement
-
8/14/2019 week08_VB_final.pdf
35/58
Declarations, Variables, and Constants
Variable - a uniquely named storage location thatcontains data that changes during programexecution
Constant - a uniquely named storage locations thatcontains data that does not change during program
execution
-
8/14/2019 week08_VB_final.pdf
36/58
Declarations, Variables, and ConstantsRules for Naming Variables
Must begin with an alphabetic characterCan not contain a period (.)or type-declarationcharacters such as
%, &, !, #, @ or $Must be unique with same scopeMust be no longer than 255 characters
Should not be a reserved word
salary sal ary x$ salary4
-
8/14/2019 week08_VB_final.pdf
37/58
Declaring Variables
Declaration statement non executable code that setsaside storage locations for future use
Local variables - declared within a procedure or functionGlobal variables - declared in the general section of the
application
-
8/14/2019 week08_VB_final.pdf
38/58
Declaring Variables.
Declare variables using the Dim statements
Dim statement - value of variable preservedonly until procedure ends
Dim variablename As datatype
-
8/14/2019 week08_VB_final.pdf
39/58
The Dim Statement
Dim variablename As datatype
WhereDim is requiredvariablename should be a descriptive nameAs is requireddatatype is one of the following types:
Boolean, Byte, Date, Integer, Long,
Single, Double, Currency, String, Object or Variant
Dim x As Integer
-
8/14/2019 week08_VB_final.pdf
40/58
Declaring Variables
Data Types Boolean - True or falseDate - From Jan 1, 100 to Dec 31, 9999Integer - Numbers without a decimal pointLong - Long integerSingle - Numbers with a decimal pointDouble - Long Single
Currency - Dollar amountsString - Character and alphanumeric dataObject - Any object reference such as Word documentVariant - default, can hold any data type
-
8/14/2019 week08_VB_final.pdf
41/58
Assigning Values to Variables
Variablename =
valueWhere
variablename is the descriptive name of the variable= is the assignment operatorvalue i the value the variable will contain
Examples:Number1 = 5FirstName = Steve Length = 17.8
Note: Order is important. Variable name always on the left, and value on theright.
Variables can be declared and initialized in the same line:Ex::Dim x As Integer=12
-
8/14/2019 week08_VB_final.pdf
42/58
Declaring Constants
Cons t constantname As datatype = value
WhereConst is required
constantname is the descriptive name of the constantAs is requireddatatype is the type of data the constant will contain= is the assignment operatorvalue is the value of the constant
Examples:
Const Pi As Single = 3.14159265358979Const MaxNumber As Integer = 100
-
8/14/2019 week08_VB_final.pdf
43/58
Functions
Function - unit of code that returns a valueBuilt-in Functions
Math.Sqrt - square rootRnd - random number generatorInt - returns integer portion of a numberVal - converts a string to a valueStr - converts a value to a string
-
8/14/2019 week08_VB_final.pdf
44/58
Application 1: Calculate the area of a circle
Inputs: radius of the circle rOutput: area of the circle
Process: Area= 2r
Application 1: Calculate the area of a
-
8/14/2019 week08_VB_final.pdf
45/58
Application 1: Calculate the area of acircle (Form View)
Circle_Area_Calculator.exe
Application 1: Calculate the area of a circle
http://localhost/var/www/apps/conversion/tmp/scratch_1/Circle_Area_Calculator/bin/Debug/Circle_Area_Calculator.exehttp://localhost/var/www/apps/conversion/tmp/scratch_1/Circle_Area_Calculator/bin/Debug/Circle_Area_Calculator.exe -
8/14/2019 week08_VB_final.pdf
46/58
Application 1: Calculate the area of a circle(Code View)
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)Handles Button1.Click
Const pi As Double= 3.14Dim r As IntegerDim area As Doubler = Val(TextBox1.Text)area = pi * r * rLabel3.Text = Str(area)
End Sub
Application 1: Calculate the area of a circle
-
8/14/2019 week08_VB_final.pdf
47/58
Application 1: Calculate the area of a circle(Run)
-
8/14/2019 week08_VB_final.pdf
48/58
Precedence Table
Operator
( )
^
*, /
+,-
If precedence of two following operators isequal, then the evaluation starts from left toright.
-
8/14/2019 week08_VB_final.pdf
49/58
Simple Examples of Equations
Equation in normal form:1. Y=3X
2. Y=X-10+3(X-Z)3. Y=
X
l Equation in VBform:
1. Y=3*X2. Y=X-10+3*(X-Z)3. Y= Math.Sqrt(X)
-
8/14/2019 week08_VB_final.pdf
50/58
Application 2: Convert from Fahrenheit Degree to Celsius
Original formula: Celsius=5/9 (Fahrenheit - 32)
Visual Basic formula: Celsius=5/9*(Fahrenheit-32)
Input: Fahrenheit
Output: Celsius
Process: Celsius=5/9*(Fahrenheit-32)
Application 2
-
8/14/2019 week08_VB_final.pdf
51/58
Application 2Convert from Fahrenheit Degree to Celsius (Form View)
Temprature_Convert.exe
Application 2
http://localhost/var/www/apps/conversion/tmp/scratch_1/Temprature_Convert/bin/Debug/Temprature_Convert.exehttp://localhost/var/www/apps/conversion/tmp/scratch_1/Temprature_Convert/bin/Debug/Temprature_Convert.exe -
8/14/2019 week08_VB_final.pdf
52/58
Application 2Convert from Fahrenheit Degree to Celsius (Code View)
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.ClickDim f As DoubleDim c As Doublef = Val(TextBox1.Text)c = 5 / 9 * (f - 32)
Label2.Text = "Celsius=" + Str(c)End Sub
The + operator is used to concatenate stringsNote:
Application 2
-
8/14/2019 week08_VB_final.pdf
53/58
Application 2Convert from Fahrenheit Degree to Celsius (Run)
Application 3
-
8/14/2019 week08_VB_final.pdf
54/58
Application 3The Wind Chill Application
Write a program that calculates the windchill temperature Inputs: Wind Speed and TemperatureOutputs: Wind Chill Temperature
Application 3
http://www.usatoday.com/weather/wchilform.htmhttp://www.usatoday.com/weather/wchilform.htmhttp://www.usatoday.com/weather/wchilform.htmhttp://www.usatoday.com/weather/wchilform.htm -
8/14/2019 week08_VB_final.pdf
55/58
Application 3The Wind Chill Application
Original formulaWC = 0.0817(3.71(V **0.5) + 5.81 - 0.25V)(T - 91.4) +
91.4Visual Basic statementWC = 0.0817 * (3.71 * Sqr(V) + 5.81 -(0.25 * V)) * (T -
91.4) + 91.4
Output: Wind Chill (WC)Input 1 T: TemperatureInput 2 V: Wind Speed
-
8/14/2019 week08_VB_final.pdf
56/58
Adding Controls and Changing their Properties
-
8/14/2019 week08_VB_final.pdf
57/58
Writing the CodePrivate Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim V As IntegerDim T As IntegerDim WC As Doublev = Val(TextBox1.Text)T = Val(TextBox2.Text)WC = 0.0817 * (3.71 * Math.Sqrt(V) + 5.81 - (0.25 * V)) * (T - 91.4) + 91.4
TextBox3.Text = Str(WC)
End Sub
-
8/14/2019 week08_VB_final.pdf
58/58
Running the Application
Wind_Chill.exe
http://localhost/var/www/apps/conversion/tmp/scratch_1/Wind_Chill/bin/Debug/Wind_Chill.exehttp://localhost/var/www/apps/conversion/tmp/scratch_1/Wind_Chill/bin/Debug/Wind_Chill.exe