Chapter 2
Creating Applications with Visual Basic
Topics
Topics
2.1 Building the Directions Application 2.2 Responding to Events 2.3 Modifying a Controls Text Property with Code 2.4 AutoSize, BorderSize, and TextAlign Properties 2.5 Displaying User Messages
Copyright 2014 Pearson Education, Inc. 2
Outline (cont.)
2.6 Clickable Images 2.7 Using Visual Studio Help Debugging Your Application
2.1
Application Specification
Application
Displays a map and written directions to the Highlander Hotel
Design a form with
Labels PictureBox Buttons
Display a map of the Highlander Hotel None Display a form Display on the form a graphic image showing a map
PictureBox
Form
10
11
12
13
14
15
New Project
16
17
Design Window
Controls
Solution Explorer Design Window
Object Box
Toolbox
Copyright 2014 Pearson Education, Inc.
Properties Window
Values Names
18
20
Save All
Sizing Handles
21
22
Build/Build Solution
Build Solution
23
Build Results
Build Results
Copyright 2014 Pearson Education, Inc. 24
25
Debug/Start
Start Debugging
26
Start Icon
Start Debugging
27
Application Running
28
29
Run mode
Executes application in the Visual Studio environment Also known as runtime
30
31
32
33
34
35
Project Organization
Solution
Project
36
Default Locations
Slide 2- 37
37
38
Open
39
40
Open
41
Re-open Project
42
Properties Window
Use Properties Window to
Select controls on Form
Object Box (dropdown list)
View/modify property values of selected object View/modify event handlers for selected object
43
44
45
46
47
Object Box
Object Box
Shows name of selected control Clicking drop-down list displays names of all objects in form Clicking name of object selects it
48
2.2
50
Controls to Add
Control Type Label Control Name lblDirections Description Displays written directions to the hotel
Button
btnDisplayDirections
Button
btnExit
51
Control Properties
Label
Name: lblDirections Text: Traveling on I-89,etc Visible: False
Button
Name: btnDisplayDirections Text: Display Directions
Button
Name: btnExit Text: Exit
Copyright 2014 Pearson Education, Inc. 52
Application Running
Slide 2- 53
53
Slide 2- 54
54
55
56
57
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load ' load directions (a very long string) lblDirections.Text = "From the airport: " & ControlChars.CrLf & "1: Start out going EAST on AIRPORT RD toward DUVAL RD/CR-110. - 0.1 miles" & ControlChars.CrLf & "2: Turn LEFT onto DUVAL RD. - <0.1 miles " & ControlChars.CrLf & "3: Turn RIGHT onto DUVAL RD/FL102 E. - 0.1 miles " & ControlChars.CrLf & "4: Merge onto I-95 S toward JACKSONVILLE. 1.6 miles " & ControlChars.CrLf & "5: Take the SR-9A S exit- EXIT 362A- toward BLOUNT ISLAND. - 0.2 miles " & ControlChars.CrLf & "6: Merge onto I-295 N. - <0.1 miles " & ControlChars.CrLf & "7: Stay STRAIGHT to go onto FL-9A S. - 13.8 miles " & ControlChars.CrLf & "8: Turn SLIGHT RIGHT onto ST JOHNS BLUFF RD S. - 1.9 miles " & ControlChars.CrLf & "9: Turn LEFT onto BEACH BLVD/US-90 E/FL-212 E. - 1.2 miles " & ControlChars.CrLf & "10: Turn LEFT onto HUFFMAN BLVD. - <0.1 miles " & ControlChars.CrLf & "11: End at 11901 Beach Blvd Jacksonville, FL 32246-6624, US" ' hide directions lblDirections.Visible = False
Application Code
Private Sub btnDisplayDirections_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplayDirections.Click lblDirections.Visible = True
End Sub
Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click ' end application Close() End Sub Private Sub btnHideDirections_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnHideDirections.Click lblDirections.Visible = False btnHideDirections.Visible = False btnDisplayDirections.Visible = True End Sub
Copyright 2014 Pearson Education, Inc. 59
60
where
ControlName.PropertyName receives value Equal sign (=) is assignment operator value is being assigned
61
62
63
64
Use keyboard
Shift + F7 to open Designer window Ctrl + Alt + 0 to open Code window
65
66
Comments
Comment (or remark)
Is short notes that you can write in the applications code to explain what code does Starts with an apostrophe (') and anything appearing after the apostrophe, to the end of the line, is ignored by the compiler
67
Comments (cont.)
May be inserted at the end of a programming statement
68
69
Colors
Colors
BackColor property sets background color ForeColor property sets text color
70
71
FixedSingle
Has single line border, Maximize, Minimize, and Close buttons May not be resized
72
73
MaximizeBox (Boolean)
Hides Maximize button when set to False
Is disabled but shown if MinimizeBox is set to True
ControlBox (Boolean)
Hides all buttons when set to False
74
75
Locking Controls
Locking controls
Prevents controls from being moved or resized during design time To lock
Right-click an empty space on form Select Lock Controls from menu
76
Using IntelliSense
IntelliSense
Provides automatic code completion as you type programming statements Use arrow keys to scroll through entries Press Tab or Enter key to accept value Example
78
VB Default Settings
2.3
Text Property
Text property
May be changed
While program is running with code Using an assignment statement
81
Assigns the string to the right of the equal sign to the Text property of lblMessage
This replaces the previous Text property of lblMessage with the new value shown
Copyright 2014 Pearson Education, Inc. 83
2.4
AutoSize Property
AutoSize property
Controls whether control will be resized to fit text Values
True (default)
Bounding box will automatically resize itself to fit the amount of text assigned to it
85
86
BorderStyle Property
BorderStyle property
Determines appearance of a labels border Values
None (default)
Label will have no border
FixedSingle
Label will be outlined with a border one pixel wide
Fixed3D
Label will have a recessed 3D appearance
87
TextAlign Property
TextAlign property
Indicates how text is aligned in control Values
TopLeft (default) TopCenter TopRight MiddleLeft MiddleCenter MiddleRight
88
89
Example
91
2.5
92
93
MessageBox.Show("Hello World!")
MessageBox dot Show string enclosed in parentheses
94
StatusStrip Control
StatusStrip control
Uses a Label to display program status information and messages to user
Ideal way to display messages that are not system critical Does not force user to click a button to clear message
95
StatusStrip
96
98
99
2.6
Clickable Images
100
101
Label1
picUSA
picCanada picUK
picAustralia
picBrazil picItaly
lblMessage
btnExit
102
103
2.7
104
Help menu
105
106
Context-Sensitive Help
Context-sensitive help
Help on a single topic that you are currently working on To initiate
Select an item you need help with in Visual Studio Then press F1 key on keyboard This launches a help screen in your Web browser
107
108
109
2.8
110
Errors
Errors
Prevent application from operating properly Most applications contain errors or bugs Types
Compile errors Runtime errors
111
Compile Errors
Compile errors
Syntax (form) of code
i.e. Misspelled keywords, incorrect use of operators or punctuation
Visual Basic informs you as soon as code is entered Code in error underlined with jagged blue line Error description appears in Error List window*
*Note: Display Error List window by selecting Error List from View menu option
Copyright 2014 Pearson Education, Inc. 112
Syntax Errors
Syntax Errors
113
114
115
Runtime Errors
Runtime error
Occurs while program is running, when VB attempts to perform an operation that can not be executed Creates incorrect results, but do not prevent program from running VB detects some and informs you about them Others you must detect yourself
Note: Always carefully check the operation of your program to be sure that it operates as required
Copyright 2014 Pearson Education, Inc. 116
Topics Covered
Topics covered
2.1 Building the Directions Application 2.2 Responding to Events 2.3 Modifying a Controls Text Property with Code 2.4 AutoSize, BorderSize, and TextAlign Properties 2.5 Displaying User Messages
Copyright 2014 Pearson Education, Inc. 117
Topics Covered
2.6 Clickable Images 2.7 Using Visual Studio Help 2.8 Debugging Your Application
118