Vb Concepts
Transcript of Vb Concepts
-
8/13/2019 Vb Concepts
1/29
Visual Basic FundamentalConcepts
-
8/13/2019 Vb Concepts
2/29
Integrated Development Enviroment
Generates startup form for new project on which toplace controls.
Features toolbox from which controls can beselected & placed on form.
Features intellisense which prompts for completionof statement or object reference.
Provides immediate syntax checking.
Provides online debugging, displaying intermediatevalues.
Provides HELP feature.
-
8/13/2019 Vb Concepts
3/29
Toolbox and Controls
Labelcontrol Textbox
control
Button
control
Toolbox
-
8/13/2019 Vb Concepts
4/29
Basic Controls
Label : Displays information through its Textproperty.
Textbox : Information Entry, Display, andTransfer through its Text Property. Usename of textbox to access information orrespond to Gotfocus event.
Button : Information Display through its TextProperty. Use name of button to respondwith subroutine to its Click event.
-
8/13/2019 Vb Concepts
5/29
Event Driven Programming
Before event driven programs, typical
applications were executed by the computerunder control of the application.
With event driven programs, the operating
system detects user interactions (mouse
click, tab key, etc.), passes them to theapplication, and a control responds by
running a subprogram.
-
8/13/2019 Vb Concepts
6/29
Controls and Associated Events
Control Event
Form Load event occurs when form is
loaded into computer memory
Button Click event occurs when user clicks
button with mouse
Textbox Lostfocus event occurs when usertabs out of box
-
8/13/2019 Vb Concepts
7/29
Click event for Avg button
Private Sub cmdAvg_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles cmdAvg.Click
'Compute average and store in text property of tbAvg control
tbAvg.Text= (CInt(tbNum1.Text) + CInt(tbNum2.Text)) / 2
tbNum1.Select()
tbNum1.SelectionStart = 0
tbNum1.SelectionLength = tbNum1.Text.Length
tbNum2.Clear()
lblStep1.Visible = True
lblStep2.Visible = False
End Sub
-
8/13/2019 Vb Concepts
8/29
Variables and Data Types
Variables are assigned values from a data
type VB data types include String, Integer,
Double, and Boolean
A Dim statement is used to declare a variable
and the data type from which its values areassigned
-
8/13/2019 Vb Concepts
9/29
Syntax of Dim and Assignment
Statements
Dim Dim as
Dim avg as double
Dim nmbr as integer
Dim flag as boolean
Assignment =
avg = (nmbr1 + nmbr2) / 2
flag = false
-
8/13/2019 Vb Concepts
10/29
Variables and Assignment Statements
Dim message As String declares string variable
'Assign string constant to messages 1 and 2Assignment statement : =
message = "Enter numbers to be & _averaged in 1st and 2nd textboxes
lblStep1.Text = messagemessage = "Click Average button"
lblStep2.Text = message
-
8/13/2019 Vb Concepts
11/29
Creating a New Project
Open Visual Studio
2005
Click Project after
Create:
-
8/13/2019 Vb Concepts
12/29
Choosing Project Language,
Template, and Project Name
Select Visual
Basic
Windows Windows
Application
Enter Project
Name (Lab1 in
example) Click OK
-
8/13/2019 Vb Concepts
13/29
ViewStartupForm and
SetProperties
-
8/13/2019 Vb Concepts
14/29
Problem Analysis & Application
Development
Analyze Problem
Example : Find averages of pairs of numbers, number1 and
number2
Input : number1, number2
Process : average = (number1 + number2) / 2
Output : average
-
8/13/2019 Vb Concepts
15/29
Project Implementation & Software
Development
Design Solution Choose forms and controls
Select labeltextbox pairs for
Input : number1 and number2 and
Output : average
Select buttons to launch processes
Average
Write code to implement process
Gets number1 and number2 values
Computes average = (number1 + number2) /2
Displays average
-
8/13/2019 Vb Concepts
16/29
Select Label Control
from Toolbox
Click on Form to
position Labeldrag if
desired.
Click on Textbox
Control from Toolbox
Click on Form to
position Textbox to right
of Label
-
8/13/2019 Vb Concepts
17/29
Name textbox tbNumber1
Holding shift key down,
select label and textbox
together
Copy with Ctrl-C
Paste with Ctrl-V (for
Number2 entry.
Select button Control and
position below labeltextbox
pair
Paste again below button
for output.
Set text properties of labels
and the button Name textbox and button
controls
Write code for button click
eventdouble click on button
to generate subroutine shell
Labeltextbox pair
Copy with Ctrl-C
-
8/13/2019 Vb Concepts
18/29
Controls after selection
Now set text properties
of labels and the button
Name textbox and
button controls
Write code for button
click eventdouble click
on button to generate
subroutine shell
-
8/13/2019 Vb Concepts
19/29
Controls after setting
text properties of labelsand the button
Name textbox and
button controls
Name of textbox for
output
Write code for button
click even by double
clicking button
Button code should
Compute & display
average
Clear input boxes
and select first box
-
8/13/2019 Vb Concepts
20/29
Write code for button click
event by double clickingbutton
Button code performs
following:
Computes average
Displays average
Clears input boxes
and selects first box
-
8/13/2019 Vb Concepts
21/29
Test Program in debugger
by switching back to designview
Click debugger button
-
8/13/2019 Vb Concepts
22/29
Copy Screen to Copy
Buffer by doing a PrintScreen
Paste onto a Word
Document
Type your name & turn in.
-
8/13/2019 Vb Concepts
23/29
Data types and Statement Types
Data
Type
Values Statement
Type
Syntax
Integer 3, 211, -42, . Declaration Dim as
Double 3.17, 1.414 Assignment =
String Hello, Bob Selection If then
End if
Boolean True, False Repetition For = to
Next
-
8/13/2019 Vb Concepts
24/29
Controls and Associated Events
Control Event
Form Load event occurs when form is loaded into computermemory
Button Click event occurs when user clicks button with mouse
Textbox Lostfocus event occurs when user tabs out of box
Listbox SelectedIndexChanged occurs when user clicks a list
item with mouse
-
8/13/2019 Vb Concepts
25/29
Problem Analysis & Application
Development
Analyze Problem
Example : Find average of list of numbers incrementally by
Keeping track of number of numbers
Keeping track of sum of numbers
Computing average after each number
Displaying numbers in a listbox
-
8/13/2019 Vb Concepts
26/29
Average of List of Grades
-
8/13/2019 Vb Concepts
27/29
-
8/13/2019 Vb Concepts
28/29
If statement for Control
If statement performs an action
() if a condition ()
is true.
Syntax :
If then
End if
-
8/13/2019 Vb Concepts
29/29
If statement Example with Else part
Syntax with Else If then
Else
End if
Example : If hours > 40 then
overtimeHours = hours40
Else overtimeHours = 0
End If