26

VISUAL BASIC Visual Basic is derived from the Basic language (Beginner’s All-Purpose Symbolic Instruction Code) Visual Basic uses an event-driven programming

Embed Size (px)

Citation preview

Page 1: VISUAL BASIC Visual Basic is derived from the Basic language (Beginner’s All-Purpose Symbolic Instruction Code) Visual Basic uses an event-driven programming
Page 2: VISUAL BASIC Visual Basic is derived from the Basic language (Beginner’s All-Purpose Symbolic Instruction Code) Visual Basic uses an event-driven programming

VISUAL BASIC

• Visual Basic is derived from the Basic language (Beginner’s All-Purpose Symbolic Instruction Code)

• Visual Basic uses an event-driven programming model.

Page 3: VISUAL BASIC Visual Basic is derived from the Basic language (Beginner’s All-Purpose Symbolic Instruction Code) Visual Basic uses an event-driven programming

PROCEDURAL V. EVENT-DRIVEN PROGRAMMING• Procedural Applications• Application execution starts with the first line of code, and

follows a predefined path through the application

• Event-Driven Applications

• Runs different code sections in response to events. Events can be triggered by the user's actions.

• The sequence of events determines the sequence in which the code runs.

• Therefore, the path through the application's code differs each time the program runs.

Page 4: VISUAL BASIC Visual Basic is derived from the Basic language (Beginner’s All-Purpose Symbolic Instruction Code) Visual Basic uses an event-driven programming

VISUAL BASIC EXAMPLE

Page 5: VISUAL BASIC Visual Basic is derived from the Basic language (Beginner’s All-Purpose Symbolic Instruction Code) Visual Basic uses an event-driven programming

VISUAL BASIC WINDOWMenu Bar

Tool Bar

Tool Box

Project Explorer Window

Properties Window

Form Layout Window

Page 6: VISUAL BASIC Visual Basic is derived from the Basic language (Beginner’s All-Purpose Symbolic Instruction Code) Visual Basic uses an event-driven programming

STEPS TO CREATE AN APPLICATION IN VISUAL

BASIC

1. Create the user interface of the application.

2. Write code that responds to actions taken in the user interface.

Page 7: VISUAL BASIC Visual Basic is derived from the Basic language (Beginner’s All-Purpose Symbolic Instruction Code) Visual Basic uses an event-driven programming

DEMONSTRATION

Page 8: VISUAL BASIC Visual Basic is derived from the Basic language (Beginner’s All-Purpose Symbolic Instruction Code) Visual Basic uses an event-driven programming

ADDING A LABEL OBJECT

• Is used to display information

• Label Properties:

Name: - identifies an object

- begin object name with lbl

Caption: - changes the text displayed in the label

Font: - changes the font, style and size

Alignment: - can be set to left, right or center

LABEL

Page 9: VISUAL BASIC Visual Basic is derived from the Basic language (Beginner’s All-Purpose Symbolic Instruction Code) Visual Basic uses an event-driven programming

ADDING A COMMAND BUTTON

• Is something the user can click on

• Label Properties:

Name: - begin object

name with cmd

Caption: - changes the text displayed in the command

button

Command Button

Page 10: VISUAL BASIC Visual Basic is derived from the Basic language (Beginner’s All-Purpose Symbolic Instruction Code) Visual Basic uses an event-driven programming

FORM PROPERTIES

• An object used to hold other objects

• Name – begin form objects with frm

• Caption – changes the text displayed in the title bar

Page 11: VISUAL BASIC Visual Basic is derived from the Basic language (Beginner’s All-Purpose Symbolic Instruction Code) Visual Basic uses an event-driven programming

EXERCISE

• GO TO PAGE 3-6

• WORK ON REVIEW NO. 2

Page 12: VISUAL BASIC Visual Basic is derived from the Basic language (Beginner’s All-Purpose Symbolic Instruction Code) Visual Basic uses an event-driven programming

PROGRAM CODE

• Is a set of instructions that tells the computer how to perform a specific task

Page 13: VISUAL BASIC Visual Basic is derived from the Basic language (Beginner’s All-Purpose Symbolic Instruction Code) Visual Basic uses an event-driven programming

EVENT PROCEDURE

• Is a block of code that executes in response to an event (ex. a mouse click)

• Ex. Done Command Button

– Unload form name or Unload Me

Page 14: VISUAL BASIC Visual Basic is derived from the Basic language (Beginner’s All-Purpose Symbolic Instruction Code) Visual Basic uses an event-driven programming

EXERCISE

• GO TO PAGE 3-10

• WORK ON REVIEW 3 AND 4

Page 15: VISUAL BASIC Visual Basic is derived from the Basic language (Beginner’s All-Purpose Symbolic Instruction Code) Visual Basic uses an event-driven programming

USING ASSIGNMENT TO CHANGE PROPERTY VALUES

• LABEL PROPERTIESCaption can be assigned textFont – can change the size

Bold and Italic are either True or Falsecan change the font name

Alignment can be assigned 0 (left justify), 1 (right justify), or 2 (center)

• COMMAND BUTTONSCaption can be assigned text

• FORM PROPERTIESCaption can be assigned text

Page 16: VISUAL BASIC Visual Basic is derived from the Basic language (Beginner’s All-Purpose Symbolic Instruction Code) Visual Basic uses an event-driven programming

EXAMPLEPrivate Sub CMDDONE_Click()

Unload FRMNAME

End Sub

Private Sub CMDGORDON_Click()

LBLNAME.Caption = "MY NAME IS GORDON"

End Sub

Private Sub CMDJOHN_Click()

LBLNAME.Caption = "MY NAME IS JOHN"

End Sub

Private Sub CMDMARY_Click()

LBLNAME.Caption = "MY NAME IS MARY"

End Sub

Page 17: VISUAL BASIC Visual Basic is derived from the Basic language (Beginner’s All-Purpose Symbolic Instruction Code) Visual Basic uses an event-driven programming

FORM LOAD EVENT PROCEDURE

• Is executed when a form is loaded into memory (when an application is started).

• Is an alternative to setting property values in the Properties window

Example

Private Sub Form_Load()

LBLNAME.CAPTION = “MY NAME IS …”

LBLNAME.ALIGNMENT = 2

End Sub

Page 18: VISUAL BASIC Visual Basic is derived from the Basic language (Beginner’s All-Purpose Symbolic Instruction Code) Visual Basic uses an event-driven programming

COMMENTING CODE

• Comments are used to explain and clarify program code for a human reader

• Has no effect on the way an application runs

• Single quotation mark ( ‘ ) must begin a comment

Page 19: VISUAL BASIC Visual Basic is derived from the Basic language (Beginner’s All-Purpose Symbolic Instruction Code) Visual Basic uses an event-driven programming

EXERCISE

• GO TO PAGE 3-13

• WORK ON REVIEW NO. 5

Page 20: VISUAL BASIC Visual Basic is derived from the Basic language (Beginner’s All-Purpose Symbolic Instruction Code) Visual Basic uses an event-driven programming

IMAGE OBJECTS

• Name – begin object name with img

• Picture – is used to display a dialog box for selecting the graphic

• Stretch – can be either True or False (True means the image is resized if the image box is resized)

• Visible – can be either True or False

IMAGE

Page 21: VISUAL BASIC Visual Basic is derived from the Basic language (Beginner’s All-Purpose Symbolic Instruction Code) Visual Basic uses an event-driven programming

EXERCISE

• GO TO PAGE 3-16

• WORK ON REVIEW 6

Page 22: VISUAL BASIC Visual Basic is derived from the Basic language (Beginner’s All-Purpose Symbolic Instruction Code) Visual Basic uses an event-driven programming

OPERATORS AND EXPRESSIONS

• Exponentiation (^ )• Multiplication ( *)• Division ( / )• Addition ( + )• Subtraction ( - )• BEDMAS• Example: lblAnswer.Caption = 3.14 *10 ^ 2

Page 23: VISUAL BASIC Visual Basic is derived from the Basic language (Beginner’s All-Purpose Symbolic Instruction Code) Visual Basic uses an event-driven programming

CREATING AN EXECUTABLE FILE

• Visual Basic includes a compiler that translates the program code into a separate executable file

• An executable file can run without Visual Basic

• Go to File and then select Make

Page 24: VISUAL BASIC Visual Basic is derived from the Basic language (Beginner’s All-Purpose Symbolic Instruction Code) Visual Basic uses an event-driven programming

EXERCISE

• Go to Page 3-18

• Complete Review 7 and 8

Page 25: VISUAL BASIC Visual Basic is derived from the Basic language (Beginner’s All-Purpose Symbolic Instruction Code) Visual Basic uses an event-driven programming

DEFINE THE FOLLOWING TERMS:

• OOP• Project• Statement• Run time• Properties window• Project explorer window• Code editor window• Design Time• Form Layout window• IDE• Interface• Tool Box• Tool Bar

Page 26: VISUAL BASIC Visual Basic is derived from the Basic language (Beginner’s All-Purpose Symbolic Instruction Code) Visual Basic uses an event-driven programming

Exercises

• Go to page 3-23

• Begin working on the exercises