Microsoft Visual Basic 2005 for Windows and Mobile

Download Report

Transcript Microsoft Visual Basic 2005 for Windows and Mobile

Microsoft Visual Basic 2005
CHAPTER 3
Program Design and Coding
3
Agenda
►Last Class: Visual
Basic 2: Program and
Graphical User
Interface Design
►Visual Basic 3:
Program Design and
Coding
►Agenda for Next Class
Chapter 3: Program Design and Coding
Presentation by: Joseph H. Schuessler, B.B.A., M.B.A., M.S., Ph.D. (ABD)
2
3
Last Class
►Visual Basic Chapter 2: Program and Graphical
User Interface Design
• Object names should be descriptive and be
preceded with the appropriate prefixes such as
‘frm’ for a form object
• The Program Development Life Cycle (PDLC)
describes the various stages that a program
goes through from inception to maintenance
• Syntax refers to a set of rules applied to the
coding language
• An apostrophe (‘) is used to denote comments
Chapter 3: Program Design and Coding
3
3
Objectives
►Change the BackColor property of an object
►Add images to a PictureBox object
►Locate and save an image from the World Wide
Web
►Import an image into the Program Resources
folder
►Size an image
Chapter 3: Program Design and Coding
Presentation by: Joseph H. Schuessler, B.B.A., M.B.A., M.S., Ph.D. (ABD)
4
3
Objectives
►Set the Visible property in the Properties window
►Set the Enabled property in the Properties
window
►Run a Visual Basic 2005 program
►Enter Visual Basic 2005 code
►Understand Visual Basic 2005 code statement
formats
Chapter 3: Program Design and Coding
Presentation by: Joseph H. Schuessler, B.B.A., M.B.A., M.S., Ph.D. (ABD)
5
3
Objectives
►Use IntelliSense to enter Visual Basic 2005 code
statements
►Using code, set the Visible property of an object
►Using code, set the Enabled property of an object
►Enter comments in Visual Basic 2005 code
Chapter 3: Program Design and Coding
Presentation by: Joseph H. Schuessler, B.B.A., M.B.A., M.S., Ph.D. (ABD)
6
3
Objectives
►Correct errors in Visual Basic 2005 code
►Write code to use the Close() procedure
►Print code
►Prepare an Event Planning Document
Chapter 3: Program Design and Coding
Presentation by: Joseph H. Schuessler, B.B.A., M.B.A., M.S., Ph.D. (ABD)
7
3
BackColor Property
►Click the Windows Form object to select it
►If necessary, scroll in the Properties window until
the BackColor property is displayed, and then
click the right column of the BackColor property
►Click the BackColor arrow. Then, if necessary,
click the Web tab to display the Web tabbed page
►Scroll until the Cornsilk color is displayed in the
list of colors
►Click Cornsilk on the color list
Chapter 3: Program Design and Coding
Presentation by: Joseph H. Schuessler, B.B.A., M.B.A., M.S., Ph.D. (ABD)
8
3
BackColor Property
Chapter 3: Program Design and Coding
Presentation by: Joseph H. Schuessler, B.B.A., M.B.A., M.S., Ph.D. (ABD)
9
3
Locating and Saving an Image
from the World Wide Web
►Open your Internet browser. Then, enter
scsite.com/vb2005/ch3/images in the
Address box and press the ENTER key
►Locate the StandardRoom.jpg image and then
right-click the image
►Click Save Picture As on the shortcut menu
►Identify the drive and folder where the image will
be stored. Enter the image file name,
StandardRoom, in the File name text box
►Click the Save button in the Save Picture dialog
box to save the image in the selected location
Chapter 3: Program Design and Coding
Presentation by: Joseph H. Schuessler, B.B.A., M.B.A., M.S., Ph.D. (ABD)
10
3
Locating and Saving an Image
from the World Wide Web
Chapter 3: Program Design and Coding
Presentation by: Joseph H. Schuessler, B.B.A., M.B.A., M.S., Ph.D. (ABD)
11
3
Importing the Image
into the Program Resources Folder
► With Visual Studio 2005 and the Hotel Room Selection
Visual Basic program open, select the picStandardRoom
PictureBox object by clicking it. Scroll in the PictureBox
Properties window until the Image property is visible. Click
the Image property name in the left list in the Properties
window
► Click the Ellipsis button in the right column of the Image
property
► Click the Import button in the Select Resource dialog box.
Then, using the features of the Open dialog box, locate
the file you want to import into the program
Chapter 3: Program Design and Coding
Presentation by: Joseph H. Schuessler, B.B.A., M.B.A., M.S., Ph.D. (ABD)
12
3
Importing the Image
into the Program Resources Folder
►Click the Open button in the Open dialog box
►With the StandardRoom file name selected in the
Project resource file list, click the OK button in the
Select Resource dialog box
Chapter 3: Program Design and Coding
Presentation by: Joseph H. Schuessler, B.B.A., M.B.A., M.S., Ph.D. (ABD)
13
3
Sizing an Image
►With the PictureBox object containing the
StandardRoom image selected, scroll in the
picStandardRoom Properties window until you
see the SizeMode property. Click the SizeMode
property name in the left column and then click
the SizeMode arrow in the right column of the
SizeMode property
►Click StretchImage in the SizeMode list
Chapter 3: Program Design and Coding
Presentation by: Joseph H. Schuessler, B.B.A., M.B.A., M.S., Ph.D. (ABD)
14
3
Sizing an Image
Chapter 3: Program Design and Coding
Presentation by: Joseph H. Schuessler, B.B.A., M.B.A., M.S., Ph.D. (ABD)
15
3
Visible Property
►If necessary, select the picStandardRoom
PictureBox object. Scroll in the Properties window
until the Visible property is displayed. Click the
Visible property name in the left column, and then
click the Visible arrow in the right column of the
Visible property
►Click False on the Visible property list
Chapter 3: Program Design and Coding
Presentation by: Joseph H. Schuessler, B.B.A., M.B.A., M.S., Ph.D. (ABD)
16
3
Visible Property
Chapter 3: Program Design and Coding
Presentation by: Joseph H. Schuessler, B.B.A., M.B.A., M.S., Ph.D. (ABD)
17
3
Enabled Property
►Select the btnSelectRoom object. Scroll in the
Properties window until the Enabled property is
displayed. Click the Enabled property name in the
left column, and then click the Enabled arrow in
the right column of the Enabled property
►Click False on the Enabled property list
Chapter 3: Program Design and Coding
Presentation by: Joseph H. Schuessler, B.B.A., M.B.A., M.S., Ph.D. (ABD)
18
3
Enabled Property
Chapter 3: Program Design and Coding
Presentation by: Joseph H. Schuessler, B.B.A., M.B.A., M.S., Ph.D. (ABD)
19
3
Running a Program
►Point to the Start Debugging button on the
Standard toolbar
►Click the Start Debugging button on the Standard
toolbar
Chapter 3: Program Design and Coding
Presentation by: Joseph H. Schuessler, B.B.A., M.B.A., M.S., Ph.D. (ABD)
20
3
Entering Visual Basic Code for Event Handling
►With Visual Studio 2005 and the Hotel Room
Selection program open and the
Form1.vb[Design] tabbed window visible, point to
the Standard Room Button object in the Windows
Form object
►Double-click the Standard Room Button object
Chapter 3: Program Design and Coding
Presentation by: Joseph H. Schuessler, B.B.A., M.B.A., M.S., Ph.D. (ABD)
21
3
Entering Visual Basic Code for Event Handling
Chapter 3: Program Design and Coding
Presentation by: Joseph H. Schuessler, B.B.A., M.B.A., M.S., Ph.D. (ABD)
22
3
Visual Basic 2005 Coding Statements
►The Visual Basic syntax specifies how each
statement must be written
►When the user clicks the Standard Room button
while the program is running, the standard room
image should be displayed in the
picStandardRoom PictureBox object
Chapter 3: Program Design and Coding
Presentation by: Joseph H. Schuessler, B.B.A., M.B.A., M.S., Ph.D. (ABD)
23
3
General Format of a Visual Basic Statement
Chapter 3: Program Design and Coding
Presentation by: Joseph H. Schuessler, B.B.A., M.B.A., M.S., Ph.D. (ABD)
24
3
IntelliSense
►IntelliSense displays all allowable entries you
can make in a Visual Basic statement each time a
dot, equal sign, or other special character
required for the statement is typed
►You must use the word, Me, followed by a dot
before the name of the object, to take full
advantage of IntelliSense
Chapter 3: Program Design and Coding
Presentation by: Joseph H. Schuessler, B.B.A., M.B.A., M.S., Ph.D. (ABD)
25
3
Entering a Visual Basic Statement
► With the code window open and the insertion point
positioned as shown in Figure 3-25 on page 131, type me
followed by a period
► To identify the correct entry, type the first letters of the
entry until the entry is selected. In this case, type pics on
your keyboard.
► When IntelliSense highlights the correct object name,
press the key on the keyboard corresponding to the entry
that is to follow the object name. In this case, press the
PERIOD key
► As with the object name in the second step, the next step
is to enter one or more characters until IntelliSense
highlights the desired property in the list. Type the letter, v
on your keyboard
Chapter 3: Program Design and Coding
Presentation by: Joseph H. Schuessler, B.B.A., M.B.A., M.S., Ph.D. (ABD)
26
3
Entering a Visual Basic Statement
►Press the key for the character that is to follow
the property name. In this case, press the
SPACEBAR on the keyboard
►Press the EQUAL SIGN key on the keyboard and
then press the SPACEBAR
►Type t on the keyboard
►Press the key for the character that is to follow
the True entry. In this case, press the ENTER key
Chapter 3: Program Design and Coding
Presentation by: Joseph H. Schuessler, B.B.A., M.B.A., M.S., Ph.D. (ABD)
27
3
Entering a Visual Basic Statement
Chapter 3: Program Design and Coding
Presentation by: Joseph H. Schuessler, B.B.A., M.B.A., M.S., Ph.D. (ABD)
28
3
Setting the Visible Property to False
►With the insertion point on the second line of the
coding window for the click event of the Standard
Room button, type me. on your keyboard
►Type picd to highlight the picDeluxeRoom entry
in the IntelliSense list
►Press the key on the keyboard for the character
that is to follow the object name. In this case,
press the PERIOD key
►Press the SPACEBAR, press the EQUAL SIGN
key, and then press the SPACEBAR
►Type f and then press the ENTER key
Chapter 3: Program Design and Coding
Presentation by: Joseph H. Schuessler, B.B.A., M.B.A., M.S., Ph.D. (ABD)
29
3
Setting the Visible Property to False
Chapter 3: Program Design and Coding
Presentation by: Joseph H. Schuessler, B.B.A., M.B.A., M.S., Ph.D. (ABD)
30
3
Enabled Property
►Type me. to display the IntelliSense list
►Type btnse until IntelliSense highlights the
btnSelectRoom entry in the list
►Type a period, type e, press the SPACEBAR,
press the EQUAL SIGN key, press the
SPACEBAR again, and then type t to select True
in the IntelliSense list
►Press the ENTER key to enter the completed
statement and place the insertion point on the
next line
Chapter 3: Program Design and Coding
Presentation by: Joseph H. Schuessler, B.B.A., M.B.A., M.S., Ph.D. (ABD)
31
3
Enabled Property
Chapter 3: Program Design and Coding
Presentation by: Joseph H. Schuessler, B.B.A., M.B.A., M.S., Ph.D. (ABD)
32
3
Comments in Program Statements
►To insert a blank line following the event code
generated by Visual Studio that begins with the
word, Private, click anywhere in that line and then
press the END key on your keyboard
►Press the ENTER key
►Type the first line of the comments, beginning
with an apostrophe, as shown in Figure 3-46 on
page 143, and then press the ENTER key
Chapter 3: Program Design and Coding
Presentation by: Joseph H. Schuessler, B.B.A., M.B.A., M.S., Ph.D. (ABD)
33
3
Comments in Program Statements
Chapter 3: Program Design and Coding
Presentation by: Joseph H. Schuessler, B.B.A., M.B.A., M.S., Ph.D. (ABD)
34
3
Same Line Comments
Chapter 3: Program Design and Coding
Presentation by: Joseph H. Schuessler, B.B.A., M.B.A., M.S., Ph.D. (ABD)
35
3
Introductory Comments
►Click to the left of the word, Public, on line 1 in
the program to place the insertion point on that
line
►Press the ENTER key one time, and then press
the UP ARROW key one time
►Type an apostrophe, a space, the text, Program
Name: and then press the TAB key one time
►Type Hotel Room Selection as the name of
the program. Then, press the ENTER key
Chapter 3: Program Design and Coding
Presentation by: Joseph H. Schuessler, B.B.A., M.B.A., M.S., Ph.D. (ABD)
36
3
Introductory Comments
Chapter 3: Program Design and Coding
Presentation by: Joseph H. Schuessler, B.B.A., M.B.A., M.S., Ph.D. (ABD)
37
3
Introductory Comments
►You can enter the remaining comments using the
same techniques
Chapter 3: Program Design and Coding
Presentation by: Joseph H. Schuessler, B.B.A., M.B.A., M.S., Ph.D. (ABD)
38
3
Correcting Errors in Code
►Using IntelliSense reduces the likelihood of
coding errors considerably, although it is still
possible to code an error
• One possible error may be forgetting an
apostrophe in a comment statement
►A build errors message means the Visual Basic
compiler detected a coding error in the program
Chapter 3: Program Design and Coding
Presentation by: Joseph H. Schuessler, B.B.A., M.B.A., M.S., Ph.D. (ABD)
39
3
Correcting Errors in Code
►Click the No button in the Microsoft Visual Studio
dialog box that informs you of a build error
►Double-click anywhere on the error line
►Click in the leftmost column on line 7 to place the
insertion point at that location
►Type an apostrophe
►Click anywhere in the code editing window
Chapter 3: Program Design and Coding
Presentation by: Joseph H. Schuessler, B.B.A., M.B.A., M.S., Ph.D. (ABD)
40
3
Correcting Errors in Code
Chapter 3: Program Design and Coding
Presentation by: Joseph H. Schuessler, B.B.A., M.B.A., M.S., Ph.D. (ABD)
41
3
Additional Click Events
►On the Form1.vb [Design] tabbed page, doubleclick the Exit Window Button object
Chapter 3: Program Design and Coding
Presentation by: Joseph H. Schuessler, B.B.A., M.B.A., M.S., Ph.D. (ABD)
42
3
Close Procedure
►With the insertion point positioned as shown in
Figure 3-64 on page 154, type me. to display the
IntelliSense list. Type cl to highlight Close in the
IntelliSense list
►Press the ENTER key
Chapter 3: Program Design and Coding
Presentation by: Joseph H. Schuessler, B.B.A., M.B.A., M.S., Ph.D. (ABD)
43
3
Printing Code
►Click File on the menu bar to display the File
menu
►Click Print on the File menu to display the Print
dialog box.
►Ensure that a check mark appears in the Include
line numbers check box if you want line numbers
on your printout. Most developers prefer line
numbers on printouts
►Make any other selections you find necessary in
the Print dialog box.
►Click the OK button in the Print dialog box to print
the code
Chapter 3: Program Design and Coding
Presentation by: Joseph H. Schuessler, B.B.A., M.B.A., M.S., Ph.D. (ABD)
44
3
Event Planning Document
Chapter 3: Program Design and Coding
Presentation by: Joseph H. Schuessler, B.B.A., M.B.A., M.S., Ph.D. (ABD)
45
3
Phase 4 – Code the Program
►After the events and tasks within the vents have
been identified, the developer is ready to code
the program
• Entering Visual Basic statements to accomplish
the tasks specified on the event planning
document
►Developer enters the code and implements the
logic
Chapter 3: Program Design and Coding
Presentation by: Joseph H. Schuessler, B.B.A., M.B.A., M.S., Ph.D. (ABD)
46
3
Summary
►Change the BackColor property of an object
►Add images to a PictureBox object
►Locate and save an image from the World Wide
Web
►Import an image into the Program Resources
folder
►Size an image
Chapter 3: Program Design and Coding
Presentation by: Joseph H. Schuessler, B.B.A., M.B.A., M.S., Ph.D. (ABD)
47
3
Summary
►Set the Visible property in the Properties window
►Set the Enabled property in the Properties
window
►Run a Visual Basic 2005 program
►Enter Visual Basic 2005 code
►Understand Visual Basic 2005 code statement
formats
Chapter 3: Program Design and Coding
Presentation by: Joseph H. Schuessler, B.B.A., M.B.A., M.S., Ph.D. (ABD)
48
3
Summary
►Use IntelliSense to enter Visual Basic 2005 code
statements
►Using code, set the Visible property of an object
►Using code, set the Enabled property of an object
►Enter comments in Visual Basic 2005 code
Chapter 3: Program Design and Coding
Presentation by: Joseph H. Schuessler, B.B.A., M.B.A., M.S., Ph.D. (ABD)
49
3
Summary
►Correct errors in Visual Basic 2005 code
►Write code to use the Close() procedure
►Print code
►Prepare an Event Planning Document
Chapter 3: Program Design and Coding
Presentation by: Joseph H. Schuessler, B.B.A., M.B.A., M.S., Ph.D. (ABD)
50
3
Next Class
►Visual Basic 4: Variables and Arithmetic
Operations
Chapter 3: Program Design and Coding
Presentation by: Joseph H. Schuessler, B.B.A., M.B.A., M.S., Ph.D. (ABD)
51
3
Questions?