home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Visual Basic 6 Programming Blue Book: The Most Complete, Hands-On Resource for Writing Programs with Microsoft Visual Basic 6!
(Publisher: The Coriolis Group)
Author(s): Peter G. Aitken
ISBN: 1576102815
Publication Date: 08/01/98

Bookmark It

Search this book:
 
Previous Table of Contents Next


Our Command Buttons will allow the user to change the size of the Text Box control or quit the program entirely.

The final step in our form design is to set a couple of properties for the form itself. Select the form by clicking on it between the controls, or by selecting Form1 from the objects list in the Properties window. (Form1 is the default name that Visual Basic assigned to the form.) Set its Caption property to Playing With Properties and its BorderStyle property to 3-Fixed Dialog. The Caption property specifies the text that is displayed in the form’s title bar; the BorderStyle property controls the form’s border and whether or not it can be resized by the user.

You have now completed the visual design part of the project. Your form should look more or less like Figure 3.8. Select Save Form1 from the File menu (or press Ctrl+S) to save the changes you’ve made to the form. You can run your program now if you like. Select Start from the Run menu (you can also click on the Start button on the toolbar or press F5). You’ll see the dialog box you just designed pop up on the screen, minus the design grid. But when you click on the various buttons, nothing happens. You have a beautiful interface, but it’s brain dead—it doesn’t respond to anything. Our next task is to add the code that will respond to user input.


Figure 3.8  The completed form.

Adding The Code

What exactly do we want the program to do? Let’s make a list:

  When the user selects one of the Option Buttons, change the color of the text in the Text Box accordingly.
  Each time the user selects the Bigger Command Button, enlarge the size of the Text Box by 20 percent.
  Each time the user selects the Smaller Command Button, reduce the size of the Text Box by 20 percent.
  When the user selects the Quit Command Button, end the program.

We’ll start with the last item in the list. The Basic statement to end a program is

End

and we want it executed when the user selects (clicks on) the Quit button. In other words, we want the End statement in the Quit button’s Click event procedure. For a refresher on event procedures in general, take another look at Chapter 1. To create this procedure, double-click on the Quit Command Button to open the code editing window with the skeleton of the event procedure already entered, as shown in Figure 3.9. (Double-clicking on an object automatically brings up its most frequently used event procedure, which happens to be Click in the case of a Command Button.)

What do I mean by skeleton? It refers to the first and last statements of the event procedure, the statements that actually define the start and the end of the procedure. The last statement, End Sub, is common to all event procedures, and does nothing more than mark the end of the procedure. Of more interest is the first statement:


Figure 3.9  Entering the code for the Command Button’s Click event.

Private Sub cmdQuit_Click()

Don’t worry about the Private and Sub keywords—all event procedures have them, and you’ll learn about them later in the book. Look at the actual name of the procedure, cmdQuit_Click. The first part is the name of the control, and the second part is the name of the event. Visual Basic names event procedures to identify both the control and the event. If you later change the control’s Name property, the names of its event procedures are automatically changed, too. You’ll be hard pressed to find anything more user-friendly than that.

If you want to work with another event or another object, you can select from the two lists at the top of the code editing window. The one on the left lists the module’s objects, and the one on the right lists the events that are supported by the selected object. But for now, it’s the Click event procedure we want. All you need to do to make this procedure functional is to add the statement End, so it reads as follows:

Private Sub cmdQuit_Click()

End

End Sub

If you run the program now (press F5 or select Start from the Run menu), you’ll see the dialog box you designed. Click on the Quit button (or press Alt+Q) to end the program. Cool! Your event procedure really works, but we have a few more event procedures to add before the project is complete.

We’ll do the Option Buttons next. We’ll start with the optBlack button. Create the Click event procedure skeleton for this button by selecting the object from the Object list at the top of the code editing window (or by returning to the form and double-clicking on the control). When this control is selected, we want the color of the text in the Text Box—its ForeColor property—set to black. But Visual Basic uses those weird numbers to represent colors. Do we have to figure out the number for black? No. We have a better way.


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions,