Visual Basic Fundamental Concepts
Visual Basic Fundamental Concepts
Concepts
Integrated Development Enviroment
Label
control Textbox
control
Button
control
Basic Controls
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 user
tabs out of box
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
Variables and Data Types
Dim
– Dim <variable> as <data-type>
Dim avg as double
Dim nmbr as integer
Dim flag as boolean
Assignment
– <variable> = <expression>
avg = (nmbr1 + nmbr2) / 2
flag = false
Variables and Assignment Statements
Select Visual
Basic
– Windows
Windows
Application
Enter Project
Name (Lab1 in
example)
Click OK
View
Startup
Form and
Set
Properties
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
Project Implementation & Software
Development
Design Solution
– Choose forms and controls
Select label – textbox 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
• Select Label Control
from Toolbox
• Click on Form to
position Label – drag if
desired.
• Click on Textbox
Control from Toolbox
• Click on Form to
position Textbox to right
of Label
• 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 label – textbox
pair • Label – textbox pair
Control Event
Form Load event occurs when form is loaded into computer
memory
Button Click event occurs when user clicks button with mouse
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
Average of List of Grades
If statement for Control