Upload
linda-bodrie
View
1.093
Download
0
Tags:
Embed Size (px)
Citation preview
Microsoft Visual Basic 2005 BASICS
Lesson 3
Events and Code
Microsoft Visual Basic 2005 BASICS 2
Objectives
Describe events and how events are key to Windows programs.
Access the Code window. Add code to a command button. Add a picture to a form.
Microsoft Visual Basic 2005 BASICS 3
Objectives (cont.)
Set picture properties. Use code to set properties. Set the Cancel and Default command
button properties. Create a stand-alone Windows program.
Microsoft Visual Basic 2005 BASICS 4
Events
Event-driven system Computer is constantly waiting for the user
to take some action With the mouse, keyboard, or other device
Action triggers an event Software in the computer attempts to find
something to do with that action
Microsoft Visual Basic 2005 BASICS 5
Events (cont.)
When you create an object It is up to you to write the code that will
handle the events. Event procedure
Code that handles a specific event
Microsoft Visual Basic 2005 BASICS 6
Accessing the Code Window
Add code to a command button Open the form that contains the command
button. Double-click the command button.
The Code window will appear.
Subroutine Section of code to perform a specific task
Microsoft Visual Basic 2005 BASICS 7
Accessing the Code Window (cont.)
Microsoft Visual Basic 2005 BASICS 8
Adding Code to a Command Button
Add code to the Code window Simply enter the code from the keyboard.
You can insert and delete text and use cut, copy, and paste.
IntelliSense technology Anticipates what you are about to key
Completes your statement for you
Visual Basic is not case sensitive.
Microsoft Visual Basic 2005 BASICS 9
Adding Code to a Command Button (cont.)
Microsoft Visual Basic 2005 BASICS 10
Adding a Picture to a Form
PictureBox control Provides a framework for displaying a
picture on a form
Microsoft Visual Basic 2005 BASICS 11
Adding a Picture to a Form (cont.)
Microsoft Visual Basic 2005 BASICS 12
Adding a Picture to a Form (cont.)
Microsoft Visual Basic 2005 BASICS 13
Adding a Picture to a Form (cont.)
Microsoft Visual Basic 2005 BASICS 14
Adding a Picture to a Form (cont.)
Microsoft Visual Basic 2005 BASICS 15
Setting Image Properties
Image controls Have an extensive set of properties that
can be changed
Microsoft Visual Basic 2005 BASICS 16
The Name Property
Name property Names the PictureBox control
The pic prefix is often used when naming PictureBox controls.
Microsoft Visual Basic 2005 BASICS 17
The SizeMode Property
SizeMode property Allows you to specify what to do when
picture and space available do not match Values
Normal Center StretchImage AutoSize
Microsoft Visual Basic 2005 BASICS 18
The SizeMode Property (cont.)
Microsoft Visual Basic 2005 BASICS 19
The SizeMode Property (cont.)
Microsoft Visual Basic 2005 BASICS 20
The Visible Property
Visible property Gives you
control over when a picture is visible to the user
Microsoft Visual Basic 2005 BASICS 21
Use Code to Set Properties
Change a property by specifying Name of the control Property Value
Microsoft Visual Basic 2005 BASICS 22
Use Code to Set Properties (cont.)
The item to the right of the dot is called a method.
In object-oriented programming You do not actually set the property. You ask an object to set its own property
by sending it a message.
Microsoft Visual Basic 2005 BASICS 23
Use Code to Set Properties (cont.)
Microsoft Visual Basic 2005 BASICS 24
Use Code to Set Properties (cont.)
Microsoft Visual Basic 2005 BASICS 25
Adding Access Keys to Command Buttons
Access key Declared in the Text property in Visual
Basic programs Allows a user to activate a command
without using a mouse
Microsoft Visual Basic 2005 BASICS 26
Setting the AcceptButton and CancelButton Form Properties
AcceptButton property Names the command button to be used
when the user presses the Enter key CancelButton property
Names the command button to be used when the user presses the Esc key
Microsoft Visual Basic 2005 BASICS 27
Summary
Windows is an event-driven environment. In an event-driven system, the user triggers events that control the work.
To control what happens when an event occurs, you must write event procedures for each event that you want to handle.
To access the Code window, double-click an object, such as a command button.
Microsoft Visual Basic 2005 BASICS 28
Summary (cont.)
Visual Basic code is written in sections called subroutines.
Adding code is much like working in a word processor. The basic text-editing features are available. In addition, Visual Studio 2005 has IntelliSense features to help format your program code.
Microsoft Visual Basic 2005 BASICS 29
Summary (cont.)
The End statement ends a program. The PictureBox tool allows you to add a
PictureBox control to a form. Using the (Name) property, you can give
a PictureBox control a name. That name will be used when you refer to the control in code.
Microsoft Visual Basic 2005 BASICS 30
Summary (cont.)
Setting the SizeMode property to StretchImage causes a picture to resize to fit the dimensions of the PictureBox control. Setting the SizeMode property to AutoSize causes a PictureBox control to resize to fit the picture loaded into it.
The Visible property controls whether an object is shown or hidden.
Microsoft Visual Basic 2005 BASICS 31
Summary (cont.)
Setting properties in code is one of the most common uses for Visual Basic code. Setting properties in code allows you to change properties while a program runs.
A command button with an ampersand (&) in front of a letter in the Text property causes that letter to become an access key.
Microsoft Visual Basic 2005 BASICS 32
Summary (cont.)
The command button named in the form’s AcceptButton property will be activated when the user presses the Enter key. The command button named in the form’s CancelButton property will be activated when the user presses the Esc key.