19.12.2012 Views

Computer Programming Concepts and Visual Basic David I. Schneider

Computer Programming Concepts and Visual Basic David I. Schneider

Computer Programming Concepts and Visual Basic David I. Schneider

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

28 <strong>Computer</strong> <strong>Programming</strong> <strong>Concepts</strong> <strong>and</strong> <strong>Visual</strong> <strong>Basic</strong><br />

Code consists of statements that carry out tasks. <strong>Visual</strong> <strong>Basic</strong> has a repertoire of over<br />

200 statements <strong>and</strong> we will use many of them in this text. In this section, we limit ourselves<br />

to statements that change properties of objects while a program is running.<br />

Properties of an object are changed in code with statements of the form<br />

objectName.property = setting<br />

where objectName is the name of the form or a control, property is one of the properties of<br />

the object, <strong>and</strong> setting is a valid setting for that object. Such statements are called assignment<br />

statements. They assign values to properties. Here are three other assignment statements.<br />

The statement<br />

txtBox.Font.Size = 12<br />

sets the size of the characters in the text box named txtBox to 12.<br />

The statement<br />

txtBox.Font.Bold = True<br />

converts the characters in the text box to boldface.<br />

The statement<br />

txtBox.Text = “”<br />

clears the contents of the text box; that is, it invokes the blank setting.<br />

Most events are associated with objects. The event clicking cmdButton is different from<br />

the event clicking picBox. These two events are specified cmdButton_Click <strong>and</strong><br />

picBox_Click. The statements to be executed when an event occurs are written in a block of<br />

code called an event procedure. The structure of an event procedure is<br />

Private Sub objectName_event()<br />

statements<br />

End Sub<br />

The word Sub in the first line signals the beginning of the event procedure, <strong>and</strong> the first line<br />

identifies the object <strong>and</strong> the event occurring to that object. The last line signals the termination<br />

of the event procedure. The statements to be executed appear between these two lines.<br />

(Note: The word Private indicates that the event procedure cannot be invoked by an event from<br />

another form. This will not concern us until much later in the text. The word Sub is an abbreviation<br />

of Subprogram.) For instance, the event procedure<br />

Private Sub cmdButton_Click()<br />

txtBox.Text = “”<br />

End Sub<br />

clears the contents of the text box when the comm<strong>and</strong> button is clicked.<br />

■ AN EVENT PROCEDURE WALKTHROUGH<br />

The form in Figure 2-13, which contains a text box <strong>and</strong> a comm<strong>and</strong> button, will be used to<br />

demonstrate what event procedures are <strong>and</strong> how they are created. Three event procedures will<br />

be used to alter the appearance of a phrase that is typed into the text box. The event procedures<br />

are txtPhrase_LostFocus, txtPhrase_GotFocus, <strong>and</strong> cmdBold_Click.<br />

Object Property Setting<br />

frmWalkthrough Caption Demonstration<br />

txtPhrase Text (blank)<br />

cmdBold Caption Make Phrase Bold<br />

FIGURE 2-13 The Interface for the Event Procedure Walkthrough

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!