An Introduction To Visual Basic 2010
An Introduction To Visual Basic 2010
CHAPTER 2 : LESSON 2
OBJECTIVES
• Create a Visual Basic 2010 Windows-based application
• Manage the windows in the Integrated Development Environment
(IDE)
• Set the properties of an object
• Add a control to a form
• Use the Label, Button, and PictureBox tools
• Use the options on the Format menu
2
OBJECTIVES (CONT'D.)
• Enter code in the Code Editor window
• Save a solution
• Start and end an application
• Print an application’s code and interface
• Write an assignment statement
• Print an application’s code and interface
• Close and open an existing solution
• Find and correct a syntax error
3
VISUAL STUDIO 2010
• Visual Basic 2010 is an object-oriented programming language
• Object: anything that can be seen, touched, or used
• Class: a pattern used to create an object
• Instance: an object created from a class; object is said to be instantiated
• Integrated Development Environment (IDE):
• Contains all the tools and features needed to create, run, and test
programs
• Includes Visual Basic, Visual C++, Visual C#, and Visual F#
4
VISUAL STUDIO 2010 (CONT'D.)
• Application: program or suite of programs
• Windows-based application:
• Has a Windows user interface
• Runs on a personal computer
• User interface: what the user sees and interacts with when using an
application
• Web-based application:
• Has a Web user interface
• Runs on a server
• Accessed with a computer browser 5
CREATING A VISUAL BASIC 2010 WINDOWS
APPLICATION
• Windows applications consist of solutions, projects, and files
• Solution: a container that stores projects and files for an entire
application
• Project: a container that stores files associated with a specific portion
of the solution
• A solution may contain one or more projects
6
SOLUTIONS, PROJECTS, AND FILES (CONT'D.)
Figure 1-2: How to start Visual Studio 2010 or Visual Basic 2010 Express
Edition
8
Figure 1-3: Visual Studio 2010 Professional startup screen
9
Figure 1-4: Visual Basic 2010 Express startup screen
10
HOW TO CREATE A VISUAL BASIC 2010
WINDOWS APPLICATION
Figure 1-5: How to create a Visual Basic 2010 Windows application (cont’d.)
12
HOW TO CREATE A VISUAL BASIC 2010
WINDOWS APPLICATION (CONT’D.)
Figure 1-7: Completed New Project dialog box in Visual Studio 2010 14
HOW TO CREATE A VISUAL BASIC 2010
WINDOWS APPLICATION (CONT’D.)
21
The Properties Window
22
The Properties Window (cont'd.)
23
THE PROPERTIES WINDOW
Figure 1-14: Properties window showing a partial listing of the form’s properties 25
PROPERTIES OF A WINDOWS FORM
26
PROPERTIES OF A WINDOWS FORM
• Pascal case:
• First letter of each word in the name is uppercase
• First part of name is object’s purpose
• Second part of name is object’s class
• Text property: controls the caption displayed on form’s title bar
• StartPosition property: determines the form’s position on the
screen when application starts
• Font: general shape of characters in text
• Recommended font is Seqoe UI font
• Point: a measure of font sizes; one point = 1/72 inch 27
THE TOOLBOX WINDOW
THE TOOLBOX WINDOW
• Toolbox:
• Contains objects that can be added to other objects, such as a form
• Each tool has an icon and a name to identify it
• Each tool represents a class from which objects, called controls, can
be created
• Controls:
• Objects displayed on a form
• Represented as icons in the toolbox
• Controls on a form can be selected, sized, moved, deleted,
locked in place on the form and unlocked 29
Figure 1-15: Toolbox window 30
THE TOOLBOX WINDOW (CONT'D.)
33
THE LABEL CONTROL (CONT'D.)
35
THE PICTURE BOX CONTROL
• Picture box control: used to display an image on a
form
• Image property: specifies the image to display
• SizeMode property: handles how the image will be
displayed
• Settings: Normal, StretchImage, AutoSize,
CenterImage, or Zoom
36
USING THE FORMAT MENU
• Format menu: provides options for manipulating controls on the
form
• Align option: aligns two or more controls by left, right, top, or bottom
borders
• Make Same Size option: makes width and/or height of two or more controls
the same
• Center in Form option: centers controls horizontally or vertically on the form
• Multi-select controls by clicking the first, then using Ctrl-click for
each additional control
• First control selected is the reference control
37
• Its size/position is used to adjust the others
THE CODE EDITOR WINDOW
• Events: user actions while program is running
• Examples: clicking, double-clicking, scrolling
• Event procedure: set of instructions to be processed when
an event occurs
• Tells the object how to respond to an event
• Code editor window: used to enter event procedure’s code
38
THE CODE EDITOR WINDOW (CONT'D.)
39
THE CODE EDITOR WINDOW (CONT'D.)
40
THE CODE EDITOR WINDOW (CONT'D.)
• Class statement: used to define a class
– Begins with Public Class <class name>
– Ends with End Class
• Class Name list box: lists the names of objects (controls) included in
the user interface
• Method Name list box: lists the events to which the selected object is
capable of responding
• When you select a control from the Class Name list box and a method
name, a code template for the event appears in the Code Editor
window
• Syntax: rules of the language 41
THE CODE EDITOR WINDOW (CONT'D.)
• Keyword: a word with special meaning in a programming language
• Event code template has a procedure header and a procedure footer
• Event’s procedure header:
• Begins with keywords Private Sub
• Procedure name includes object name and event name
• Handles clause indicates for which objects’ events this code will execute
• Sub procedure: block of code that performs a task
42
THE CODE EDITOR WINDOW (CONT'D.)
Figure 1-21: Code template for the exitButton’s Click event procedure
43
THE ME.CLOSE() INSTRUCTION
• Me.Close() instruction: closes the current form at run time
• If the current form is the only form, the application is terminated
• Me keyword: refers to the current form
• Method: predefined VB procedure that can be invoked (called) when
needed
• Sequential processing: each line is executed in sequence
• Also called a sequence structure
44
THE ME.CLOSE() INSTRUCTION (CONT'D.)
45
SAVING A SOLUTION
• An asterisk appears on the designer and Code Editor tabs if a change
was made since the last time the solution was saved
46
STARTING AND ENDING AN APPLICATION
• Startup form: the form to be displayed when the application starts
49
STARTING AND ENDING AN APPLICATION
(CONT'D.)
51
STARTING AND ENDING AN APPLICATION
(CONT'D.)
52
ASSIGNING A VALUE TO A
PROPERTY
DURING RUN TIME
• Properties window is used to set property values at design time
• Assignment statement: assigns a value to a variable or
property of a control
• Used to set property values at run time
• String: zero or more characters enclosed in quotation marks
• Assignment operator: the = sign
• Value of the expression on the right of the = sign is assigned to
the object and property on the left of the = sign
53
USING AN ASSIGNMENT STATEMENT
(CONT'D.)
Figure 1-30: How to print the code and interface during design time 55
CLOSING THE CURRENT SOLUTION
• Closing a solution closes all projects and files in that solution
• You are prompted to save any files that have unsaved changes
56
OPENING AN EXISTING SOLUTION
• Only one solution can be open at any one time
• If a solution is already open, opening a different one will close
the currently open solution
59
CODING ERRORS (CONT’D.)
60
CODING ERRORS (CONT’D.)
62
PROGRAMMING TUTORIAL 1
65
SUMMARY
66
SUMMARY (CONT'D.)
68
SUMMARY (CONT'D.)
• Controls on a form can be selected, sized, moved, deleted, or locked at design time
• Label control: contains text that a user cannot edit
• Button control: performs an immediate action when clicked
• Picture box control: displays an image on a form
• Format menu provides options for aligning and sizing controls on a form
• Event procedure: the code that tells an object how to respond to an event
69
SUMMARY (CONT'D.)
• Use the Class Name and Method Name list boxes to select an object and event to
code
• Code Editor: provides code templates for each object’s event procedures
• Me.Close() instruction: can terminate an application
• Computer automatically creates an executable file when you start a Visual Basic
application in the IDE
• Use an assignment statement to assign a value to a property during run time
70
SUMMARY (CONT'D.)
• You should print your application’s code and its user interface
• Closing a solution closes all projects and files in it
• The process of locating and correcting errors (bugs) in a program is called
debugging
71