Title: Microsoft VB 2005: Reloaded, Advanced
1Microsoft VB 2005 Reloaded, Advanced
- Chapter 1
- Reviewing Microsoft Visual Basic 2005 Reloaded
Part I
2Objectives
- Design and create a user interface for an
application - Use variables and constants in methods to perform
calculations - Create pseudocode, flowcharts, and program code
for the sequence, selection, and repetition
structures - Create a project, solution, and application in
Visual Basic 2005
3An Introduction to Visual Basic 2005
- Get started using the Visual Studio 2005
integrated development environment (IDE)
4Programmers and Programming Languages
- Programmer categories
- Systems programmers
- Write and maintain programs for fundamental
operating functions - Application programmers
- Write and maintain programs to handle real-world
tasks - Programming languages
- Machine languages
- Assembly languages
- High-level languages
5Programmers and Programming Languages (continued)
6Procedure-Oriented and Object-Oriented Programming
- Procedure-oriented programming
- Program code is broken into groups of procedures
- Examples FORTRAN, Pascal, and C
- Object-oriented programming (OOP)
- Gives greater attention to various interacting
objects - Use their own internal data and procedures to
accomplish application objectives - Examples Visual Basic, Java, and C
7Procedure-Oriented and Object-Oriented
Programming (continued)
- Object-oriented terminology
- Object
- Instance of a class in computer memory
- Class
- Unit of program code that defines a category of
objects - Attributes (also called properties)
- Characteristics that describe an object
- Behaviors (also called procedures or methods)
- Operations that an object is capable of performing
8Procedure-Oriented and Object-Oriented
Programming (continued)
- Object-oriented terminology (continued)
- Instantiation
- Process of creating an object in computer memory
- Abstraction
- Means using the properties and methods of objects
- Without worrying about how they are implemented
- Inheritance
- Process by which a derived class can
automatically reuse the properties and methods of
a base class
9Procedure-Oriented and Object-Oriented
Programming (continued)
- Object-oriented terminology (continued)
- Polymorphism
- Enables one method to have several different
implementations - Depending on the type of object calling the method
10Procedure-Oriented and Object-Oriented
Programming (continued)
11Visual Studio 2005
- Microsofts IDE for creating an object-oriented
computer application - Uses the following languages
- Visual Basic
- Visual C
- Visual C
- Visual J
- Integrated development environment (IDE)
- Tools for creating, compiling, and testing
programs
12Visual Studio 2005 (continued)
- User interface
- Enables user to interact with the application
- Visual Studio 2005 application types
- Console application
- Windows-based application
- Web-based application
- Microsoft .NET Framework 2.0
- Development and execution environment
- Allows different programming languages and
libraries to work together to create applications
13Visual Studio 2005 (continued)
- The common language runtime (CLR)
- Core runtime engine of Microsoft .NET Framework
- Compiles and executes the IL for a variety of
computer platforms - Solutions, projects, and files
- Components of a Visual Studio 2005 application
- Solution contains projects for an application
- Project contains files for a specific part
- Starting Microsoft Visual Studio 2005
14Visual Studio 2005 (continued)
15Visual Studio 2005 (continued)
16Creating a Visual Basic Windows-Based Application
17Creating a Visual Basic Windows-Based Application
(continued)
18Managing the Windows in the IDE
- Click View in the menu bar to open windows
- Auto Hide option
- Hides the window but displays a tab for the
window on the left or right side of the IDE - Moving the mouse over this tab causes the window
to temporarily appear - Undock a window
- By double-clicking its title bar
- Redock by dragging to sides or corners of the IDE
19The Windows Form Designer Window
- Contains a Windows Form object
- Title bar and the minimize/maximize/close buttons
- Form is used to create a graphical user interface
(GUI) for the application - And holds other objects
20The Solution Explorer Window
- Lists the projects, folders, and files within a
solution - Within each project are a My Project node
- Double-clicking the My Project node opens the
Project Designer window - Contains a wide variety of project management
tools - .vb files
- Visual Basic source files
- Code is written by you or automatically by the
IDE - Form1.vb is a form file
21The Properties Window
- Objects have attributes or properties
- A Visual Basic file is an object
- Its properties are displayed in the Properties
window - Properties of a Windows Form object
- Object box contains the name of the object
followed by System.Windows.Forms.Form - Namespace is a name given to a collection of
related classes - System namespace contains all the basic classes
for creating Visual Basic applications
22The Properties Window (continued)
- Properties of a Windows Form object
- Name meaningful name given to the form
- Text text that appears in the forms title bar
- StartPosition where the form will be positioned
on the screen - Notes on the notation used
- Hungarian notation (old notation)
- frmMain
- New notation
- MainForm (using Pascal case)
23The Toolbox Window
- Also called the Toolbox
- Contains tools for building an application
- Controls
- Another name for objects added to a form
- Can be dragged from the Toolbox to the form
- And then manipulated to meet the needs of the
application - The Label tool
- Can create a label control on a form
- A label displays text in the form
24The Toolbox Window (continued)
25The Toolbox Window (continued)
26The Toolbox Window (continued)
- Notation
- Control names are written using camel notation
- Examples
- commissionLabel
- calculateButton
- The Button tool
- Creates a button control on a form
- When the user clicks a button on a form, a
specific action (as defined by the programmer)
occurs
27The Code Editor Window
- Event example
- Clicking a button in a form
- Event procedure
- Tells the application what to do when an event
occurs - Written in the Code Editor window
- Open Code Editor window
- Click View and then Code in the menu
28The Code Editor Window (continued)
29The Code Editor Window (continued)
- Creating a Sub procedure for a control
- Sub procedure
- Block of code that performs specific tasks
- Select an object from the Class Name list box
- And an event from the Method Name list box
- Keyword
- Also called a reserved word
- Has a special meaning in a programming language
30The Code Editor Window (continued)
Procedure handles Click event for exitButton
Procedure header
Object
Event
31The Code Editor Window (continued)
- The Me.Close method
- Causes a form to close and an application to
terminate - Me refers to the specific instance of a class or
structure in which the code is currently
executing
32Debugging an Application
- Startup form
- Form that is automatically displayed when an
application is started - Specified using the Solution Explorer window
- Save an application
- Click the Save All button in the Standard toolbar
or - Click File on the menu bar and then click Save
All - Test an application
- Click Start Debugging button in Standard toolbar
or - Press the F5 key on the keyboard
33Debugging an Application (continued)
- Test an application (continued)
- Saves the application and creates an .exe file
- .exe file is stored in the projects bin folder
- End an application
- Click Stop Debugging button in Standard toolbar
or - Click Close button in the applications title bar
34Printing Your Code
- Steps
- Make the Code Editor window active
- Click the plus boxes only for code you want to
print - Click File on the menu bar, click Print, click OK
35Closing and Opening a Solution
- Close the current solution
- Click File on the menu bar and click Close
- Open a solution
- Click File on the menu bar, then click Open File
- Locate the solution filename (with the .sln
extension) and double-click it - Only one solution can be open in a single Visual
Studio IDE at any one time - You can open several applications in separate
Visual Studio IDEs
36Creating a User Interface
- Explore how to plan an application and design a
user interface - Using the Visual Studio 2005 IDE
37Planning an Application
- Necessary for successful application development
- TOE (Task, Object, Event) chart
- One way of planning a Visual Basic application
- Helps you
- Identify the tasks that the application should
perform - Identify the objects to which the tasks will be
assigned - Identify the events that will trigger these
objects to perform the tasks - After TOE chart is done, you can design the GUI
38Planning an Application (continued)
39Identifying the Application Tasks
- Consider the following questions
- What information should be displayed in the
interface and/or printed to the printer? - What information will the user need to enter into
the user interface? - What information will the application need to
calculate? - How will the user end the application?
- Will previous information need to be cleared from
the screen before new information is entered?
40Identifying the Objects
- Objects perform tasks
- Form object collects information on the screen
- TextBox objects are used to enter data
- Label objects are used to display data
- Button objects are needed to perform specific
actions
41Identifying the Events
- Visual Basic automatically handles data entry for
text boxes - As well as data display for text boxes and labels
- You must identify events for the buttons
42Identifying the Events (continued)
43Identifying the Events (continued)
44Designing the User Interface
- You need to design exactly where the controls
will be located within the form - Primary window
- Main user interface
- Dialog boxes
- Non-resizable windows that support and supplement
a users activities in the primary window - FormBorderStyle property of the Form object
- Controls the appearance and functionality of an
applications primary window
45Designing the User Interface (continued)
- Arranging the controls
- Group together related controls using
- GroupBox control
- Creates a border around a group of controls and a
caption for the group - Panel control
- Can have scroll bars
- TableLayoutPanel control
- Provides a table structure for controls
- These controls are found in the Containers
section of the Toolbox
46Designing the User Interface (continued)
47Designing the User Interface (continued)
- Aligning and sizing the controls
- Select them
- Click Format in the menu bar
- Choose one of several options in the Format menu
- Including graphics in the user interface
- See Figure 1.13
- Graphics in a user interface should be limited
- Including different fonts in the user interface
- An objects Font property controls the objects
text type, style, and size
48Designing the User Interface (continued)
49Designing the User Interface (continued)
- Including color in the user interface
- Use this feature sparingly
- Limit colors to three that are complementary
- Assigning access keys
- Access key allows an object to be selected using
a combination of the Alt key with a letter or
number - Enter an ampersand () just before the desired
letter in the controls Text property - Setting the TabIndex property
- Determines the order in which a control receives
focus when the Tab key or an access key is used
50Designing the User Interface (continued)
- Setting the TabIndex property (continued)
- Focus is the ability of an object to accept user
input - Change the tab order by selecting a control and
changing its TabIndex property - Visual Basic assigns this value as controls are
added - Easier way
- Click View in the menu bar, then click Tab Order
- Use mouse to click the controls in the desired
tab order
51Designing the User Interface (continued)
52Defining Default and Cancel Buttons
- Default button
- Always selected when the user presses the Enter
key - Created by setting the forms AcceptButton
property to the name of the button - Cancel button
- Closes the application
- Created by setting the forms CancelButton
property to the name of the button
53Variables, Constants, Methods, and Calculations
- Learn how to
- Work with variables
- Create arithmetic expressions
- Provide application output
- Also study techniques for planning, creating,
testing, and debugging an application
54Variables
- Variables
- Simply computer memory locations that can store
specific types of data and are given special
names - Selecting a data type for a variable
- Data type is the kind of data a variable can
store - Integer data types Integer, Short, and Long
- Floating-point data types Single and Double
- Other special data types Boolean, Byte, Char,
Decimal, and String - Default data type Object
55Variables (continued)
- Selecting a name for a variable
- Identifiers are variable names
- Names should be very descriptive, indicating the
variables purpose - Use camel case for variable names
- Visual Basics syntax (rules of a programming
language) for variable names - Must begin with a letter or an underscore
- Only letters, numbers, and the underscore (no
spaces) - Must not be a Visual Basic keyword word
56Declaring a Variable
57Assigning Data to an Existing Variable
- Assignment statement
- variablename value
- A variable may be assigned a literal constant
- Value does not change while an application is
running - Variables must be assigned values of the correct
data type - Literal type character
- Can change a numeric literal constants data type
to match that of a variable
58Assigning Data to an Existing Variable (continued)
- Using the TryParse method
- Data type in Visual Basic is defined by a class
- Method
- Part of a class that performs a specific task
- TryParse method
- Converts a string of numeric characters into an
actual numeric value of a particular numeric data
type - Using the Convert class
- Converts numbers from one data type to another or
a string to a number
59Assigning Data to an Existing Variable (continued)
60Assigning Data to an Existing Variable (continued)
61Assigning Data to an Existing Variable (continued)
62Writing Arithmetic Expressions
- Arithmetic expressions
- Perform calculations in Visual Basic
- Contain arithmetic operators
- Precedence numbers
- Order of operation in an expression
- When operators have the same precedence numbers,
operators are applied from left to right - Parentheses override normal operator precedence
63Writing Arithmetic Expressions (continued)
64The Scope and Lifetime of a Variable
- Scope of a variable
- Context or boundary within an application where
the variable is declared and can be used - Types module, procedure, and block scope
- Lifetime of a variable
- Length of time the variable remains in memory
- Static variable
- Procedure-level variable that retains its value
even when the procedure in which it is declared
ends
65The Scope and Lifetime of a Variable (continued)
66Named Constants
- Named constant
- Like a variable, is a named memory location
- The value of a named constant cannot be changed
while the application is running - Useful for holding constants that are used
repeatedly throughout an application - Or that may need to be changed by the programmer
over a period of time
67Named Constants (continued)
68Providing for Application Output
- Assigning a value to the property of a control
- See Figure 1.21
- Using the Focus method
- See Figure 1.22
- Formatting numeric output
- See Figure1.23
- Option Explicit and Option Strict
- Option Explicit On
- Using an undeclared variable will then result in
an error - Option Strict On
- Prevents implicit type conversion
69Providing for Application Output (continued)
70Providing for Application Output (continued)
71Providing for Application Output (continued)
72Coding the Skate-Away Sales Application
- Using pseudocode to plan a procedure
- Pseudocode
- Text that describes a procedure
- Using a flowchart to plan a procedure
- Flowchart
- Uses standard symbols to graphically depict a
procedure
73Coding the Skate-Away Sales Application
(continued)
74Coding the calcButton, clearButton, and
exitButton Controls Click Event Procedures
75Coding the calcButton, clearButton, and
exitButton Controls Click Event Procedures
(continued)
76Testing and Debugging the Application
- Debugging
- Locating and correcting errors in a program
- Syntax errors
- Violations of a programming languages syntax
- Visual Basic will find syntax errors for you
- Logic errors
- Result when an application provides incorrect
output - Due to mistakes in defining the logic of an
application
77Testing and Debugging the Application (continued)
78Making Decisions in Programs
- Ability of an application to make decisions
- Vital part of program functionality
79The Selection Structure
- Three basic structures used in programming
- Sequence structure
- Selection structure
- Repetition structure
- Selection structure
- Different sets of instructions are processed
depending on certain conditions in the program
80Writing Pseudocode for the If and If/Else
Selection Structures
81Flowcharting the If and If/Else Selection
Structures
82Coding the If and If/Else Selection Structures
83Comparison Operators
- Six comparison operators
- , gt, gt, lt, lt, and ltgt
- Logical expression can contain both arithmetic
operators and comparison operators - Arithmetic operators always take precedence over
comparison operators - All comparison operators have the same precedence
- Executed left to right unless parentheses are used
84Using the ToUpper and ToLower Methods
85Logical Operators
- Logical operators (or Boolean operators)
- Allow you to create compound logical expressions
- Can be used as conditions in If and If / Else
selection structures - Operators (in order of precedence)
- Not, And, and AndAlso Or and OrElse and Xor
- Short-circuit evaluation
- When Visual Basic determines logical outcome of a
compound expression, it ceases to evaluate the
other parts of the expression
86Performing Data Validation
- Data validation
- To verify that users are entering valid data
- Extremely important
- Use If or If/Else selection structures
- String.IsNullorEmpty(string)
- Checks if a text box contains data before
proceeding with processing - Returns True or False
87Modifying the Skate-Away Sales Application
- Problem
- User can enter a non-numeric character in either
the blueTextBox or the yellowTextBox - Zeros appear for total skateboards and total
price - Modify application to display an error message if
one of these text boxes does not contain an
integer
88The MessageBox.Show Method
- Syntax
- MessageBox.Show(text, caption, buttons,
icon,defaultButton) - Displays a dialog box that contains a text
89The MessageBox.Show Method (continued)
90The MessageBox.Show Method (continued)
91Nested Selection Structures
- Nested selection structure
- A selection structure, located within an outer
selection structure - Allows for multiple decisions by including
additional selection structures within the true
or false path
92Nested Selection Structures (continued)
93The If/ElseIf/Else Structure
- Facilitates decisions with more than two choices
- Equivalent to multiple nested If/Else structures
94The Case Selection Structure
95The Case Selection Structure (continued)
96Generating Random Numbers
- Pseudo-random number generator
- Produces a sequence of numbers that meet certain
statistical requirements for randomness
97Generating Random Numbers (continued)
98Repeating Program Instructions
- Objectives
- Review how to perform looping
- Work with several different kinds of Visual Basic
controls related to the repetition structure
99The Repetition Structure
- Repetition structure (also called a loop)
- Repeats a block of code (called the loop body)
while a certain condition is true - Types of repetition structures
- Pretest loop
- Evaluates condition before loop body executes
- Posttest loop
- Evaluates condition after loop body executes once
100The Repetition Structure (continued)
- Repetition statements
- For...Next
- Do...Loop
- For Each...Next
101The ForNext Statement
102The ForNext Statement (continued)
103The Financial.Pmt Method
- Calculates periodic payment on loan or investment
- Often used with a loop
- Syntax
- Financial.Pmt(Rate, NPer, PV, FV, Due)
- The monthly payment calculator application
- Illustrates the use of a ForNext loop
- Selecting the existing text in a text box
- principalTextBox.SelectAll() selects the contents
of the text box
104The Financial.Pmt Method (continued)
- Coding a controls TextChanged Event procedure
- In the Code Editor window, select the
principalTextBox control - And the TextChanged method
105Using a ListBox Control in an Interface
- List box displays a list of choices from which
the user can select zero, one, or more choices - ListBox class defines the ListBox control
- ListBox.ObjectCollection class defines the
collection of items contained in the ListBox - Properties
- Item
- SelectedItems
- SelectedIndex
- SelectionMode
106Using a ListBox Control in an Interface
(continued)
- The SelectedItems and SelectedIndex properties
- Each item in a list box has an index starting
with 0 - SelectedItems equals the selected item name
- SelectedIndex equals the selected item index
- SelectionMode determines how many items can be
selected at the same time - The SelectedValueChanged and SelectedIndexChanged
events - Occur each time an item is selected in a list box
107Modifying the Monthly Payment Calculator
Application
- Modify to use a list box that contains the
possible terms of 2, 3, 4, or 5 years for the loan
108Modifying the Monthly Payment Calculator
Application (continued)
109Using a Combo Box in an Interface
- Combo box
- Allows the user to select from a list of choices
- Can contain a text field that allows the user to
type an entry that is not on the list - Styles of combo boxes in Visual Basic (controlled
by the DropDownStyle property) - Simple
- DropDown
- DropDownList
110The DoLoop Statement
- Used to code both a pretest loop and a posttest
loop
111The DoLoop Statement (continued)
112The DoLoop Statement (continued)
113Using Counters and Accumulators
- Counter
- Numeric variable for counting something
- Accumulator
- Numeric variable for accumulating (adding) items
- Counters and accumulators are usually initialized
by setting their initial values to zero - Updated by adding values to them for each
iteration of a loop
114The InputBox Function
- Function
- Predefined procedure that performs a specific
task - And then returns a value after completing the
task - InputBox function
- Displays a predefined dialog box that contains a
message - Along with an OK button, a Cancel button, and an
input area in which the user can enter information
115The InputBox Function (continued)
116The InputBox Function (continued)
117The Sales Express Application
118Summary
- Programming language progression
- Machine languages
- Assembly languages
- High-level languages
- High-level languages types
- Procedure-oriented
- Object-oriented
- The Microsoft .NET Framework 2.0 is a platform on
which .NET applications are created - Applications created in Visual Studio are
composed of solutions, projects, and files
119Summary (continued)
- Create application GUI in the Windows Form
Designer window - Variables and named constants are computer memory
locations that store data - Types of statements
- Declaration statement
- Assignment statement
- Variable properties
- Scope
- Lifetime
120Summary (continued)
- Selection structures
- If
- If/Else
- If/ElseIf/Else
- Case Switch
- Repetition structures
- ForNext
- DoLoop
- For EachNext