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)
4Procedure-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
5Procedure-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
6Procedure-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
7Procedure-Oriented and Object-Oriented
Programming (continued)
8Visual 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
9Visual 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
10Visual 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
11Visual Studio 2005 (continued)
12Visual Studio 2005 (continued)
13Creating a Visual Basic Windows-Based Application
14Creating a Visual Basic Windows-Based Application
(continued)
15Managing 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
16The 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
17The 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
18The 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
19The 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)
20The 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
21The Toolbox Window (continued)
22The Toolbox Window (continued)
23The 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
24The 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
25The Code Editor Window (continued)
26The 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
27The Code Editor Window (continued)
Procedure handles Click event for exitButton
Procedure header
Object
Event
28The 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
29Debugging 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
30Debugging 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
31Printing 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
32Closing 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
33Creating a User Interface
- Explore how to plan an application and design a
user interface - Using the Visual Studio 2005 IDE
34Planning 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
35Planning an Application (continued)
36Identifying 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?
37Identifying 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
38Identifying 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
39Identifying the Events (continued)
40Identifying the Events (continued)
41Designing 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
42Designing 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
43Designing the User Interface (continued)
44Designing 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
45Designing the User Interface (continued)
46Designing 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
47Designing 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
48Designing the User Interface (continued)
49Defining 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
50Variables, 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
51Variables
- 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
52Variables (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
53Declaring a Variable
54Assigning 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
55Assigning 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
56Assigning Data to an Existing Variable (continued)
57Assigning Data to an Existing Variable (continued)
58Assigning Data to an Existing Variable (continued)
59Writing 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
60Writing Arithmetic Expressions (continued)
61The 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
62The Scope and Lifetime of a Variable (continued)
63Named 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
64Named Constants (continued)
65Providing 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
66Providing for Application Output (continued)
67Providing for Application Output (continued)
68Providing for Application Output (continued)
69Making Decisions in Programs
- Ability of an application to make decisions
- Vital part of program functionality
70The 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
71Coding the If and If/Else Selection Structures
72Comparison 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
73Using the ToUpper and ToLower Methods
74Logical 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
75Performing 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
76The MessageBox.Show Method
- Syntax
- MessageBox.Show(text, caption, buttons,
icon,defaultButton) - Displays a dialog box that contains a text
77The MessageBox.Show Method (continued)
78The MessageBox.Show Method (continued)
79The If/ElseIf/Else Structure
- Facilitates decisions with more than two choices
- Equivalent to multiple nested If/Else structures
80The Case Selection Structure
81The Case Selection Structure (continued)
82Generating Random Numbers
- Pseudo-random number generator
- Produces a sequence of numbers that meet certain
statistical requirements for randomness
83Generating Random Numbers (continued)
84Repeating Program Instructions
- Objectives
- Review how to perform looping
- Work with several different kinds of Visual Basic
controls related to the repetition structure
85The 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
86The Repetition Structure (continued)
- Repetition statements
- For...Next
- Do...Loop
- For Each...Next
87The ForNext Statement
88The 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
89The Financial.Pmt Method (continued)
- Coding a controls TextChanged Event procedure
- In the Code Editor window, select the
principalTextBox control - And the TextChanged method
90Using 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
91Using 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
92Modifying 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
93Modifying the Monthly Payment Calculator
Application (continued)
94Using 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
95The DoLoop Statement
- Used to code both a pretest loop and a posttest
loop
96The DoLoop Statement (continued)
97The DoLoop Statement (continued)
98The 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
99The InputBox Function (continued)
100The InputBox Function (continued)
101The Sales Express Application
102Summary
- 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
103Summary (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
104Summary (continued)
- Selection structures
- If
- If/Else
- If/ElseIf/Else
- Case Switch
- Repetition structures
- ForNext
- DoLoop
- For EachNext