32
Microsoft Visual Basic 2005 BASICS Lesson 3 Events and Code

Microsoft Visual Basic 2005 BASICS Lesson 3 Events and Code

Embed Size (px)

DESCRIPTION

Microsoft Visual Basic 2005 BASICS3 Objectives (cont.) Set picture properties. Use code to set properties. Set the Cancel and Default command button properties. Create a stand-alone Windows program.

Citation preview

Page 1: Microsoft Visual Basic 2005 BASICS Lesson 3 Events and Code

Microsoft Visual Basic 2005 BASICS

Lesson 3Events and Code

Page 2: 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.

Page 3: Microsoft Visual Basic 2005 BASICS Lesson 3 Events and Code

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.

Page 4: Microsoft Visual Basic 2005 BASICS Lesson 3 Events and Code

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

Page 5: Microsoft Visual Basic 2005 BASICS Lesson 3 Events and Code

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

Page 6: Microsoft Visual Basic 2005 BASICS Lesson 3 Events and Code

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

Page 7: Microsoft Visual Basic 2005 BASICS Lesson 3 Events and Code

Microsoft Visual Basic 2005 BASICS 7

Accessing the Code Window (cont.)

Page 8: Microsoft Visual Basic 2005 BASICS Lesson 3 Events and Code

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.

Page 9: Microsoft Visual Basic 2005 BASICS Lesson 3 Events and Code

Microsoft Visual Basic 2005 BASICS 9

Adding Code to a Command Button (cont.)

Page 10: Microsoft Visual Basic 2005 BASICS Lesson 3 Events and Code

Microsoft Visual Basic 2005 BASICS 10

Adding a Picture to a Form

PictureBox control Provides a framework for displaying a

picture on a form

Page 11: Microsoft Visual Basic 2005 BASICS Lesson 3 Events and Code

Microsoft Visual Basic 2005 BASICS 11

Adding a Picture to a Form (cont.)

Page 12: Microsoft Visual Basic 2005 BASICS Lesson 3 Events and Code

Microsoft Visual Basic 2005 BASICS 12

Adding a Picture to a Form (cont.)

Page 13: Microsoft Visual Basic 2005 BASICS Lesson 3 Events and Code

Microsoft Visual Basic 2005 BASICS 13

Adding a Picture to a Form (cont.)

Page 14: Microsoft Visual Basic 2005 BASICS Lesson 3 Events and Code

Microsoft Visual Basic 2005 BASICS 14

Adding a Picture to a Form (cont.)

Page 15: Microsoft Visual Basic 2005 BASICS Lesson 3 Events and Code

Microsoft Visual Basic 2005 BASICS 15

Setting Image Properties

Image controls Have an extensive set of properties that

can be changed

Page 16: Microsoft Visual Basic 2005 BASICS Lesson 3 Events and Code

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.

Page 17: Microsoft Visual Basic 2005 BASICS Lesson 3 Events and Code

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

Page 18: Microsoft Visual Basic 2005 BASICS Lesson 3 Events and Code

Microsoft Visual Basic 2005 BASICS 18

The SizeMode Property (cont.)

Page 19: Microsoft Visual Basic 2005 BASICS Lesson 3 Events and Code

Microsoft Visual Basic 2005 BASICS 19

The SizeMode Property (cont.)

Page 20: Microsoft Visual Basic 2005 BASICS Lesson 3 Events and Code

Microsoft Visual Basic 2005 BASICS 20

The Visible Property

Visible property Gives you

control over when a picture is visible to the user

Page 21: Microsoft Visual Basic 2005 BASICS Lesson 3 Events and Code

Microsoft Visual Basic 2005 BASICS 21

Use Code to Set Properties

Change a property by specifying Name of the control Property Value

Page 22: Microsoft Visual Basic 2005 BASICS Lesson 3 Events and Code

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.

Page 23: Microsoft Visual Basic 2005 BASICS Lesson 3 Events and Code

Microsoft Visual Basic 2005 BASICS 23

Use Code to Set Properties (cont.)

Page 24: Microsoft Visual Basic 2005 BASICS Lesson 3 Events and Code

Microsoft Visual Basic 2005 BASICS 24

Use Code to Set Properties (cont.)

Page 25: Microsoft Visual Basic 2005 BASICS Lesson 3 Events and Code

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

Page 26: Microsoft Visual Basic 2005 BASICS Lesson 3 Events and Code

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

Page 27: Microsoft Visual Basic 2005 BASICS Lesson 3 Events and Code

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.

Page 28: Microsoft Visual Basic 2005 BASICS Lesson 3 Events and Code

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.

Page 29: Microsoft Visual Basic 2005 BASICS Lesson 3 Events and 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.

Page 30: Microsoft Visual Basic 2005 BASICS Lesson 3 Events and 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.

Page 31: Microsoft Visual Basic 2005 BASICS Lesson 3 Events and Code

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.

Page 32: Microsoft Visual Basic 2005 BASICS Lesson 3 Events and Code

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.