Click Here!
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


Remember that we have already set the color of each Option Button to the corresponding color. The number we need is therefore already present in the Option Button’s ForeColor property. All we need to do is copy that number from the Option Button’s ForeColor property to the Text Box’s ForeColor property, and we accomplish our task without having to worry about the exact color number. Here’s the event procedure with the single necessary line of code added:

Private Sub optBlack_Click()

Text1.ForeColor = optBlack.ForeColor

End Sub


Note: As you create these event procedures, be careful with the spelling and punctuation in the code; the computer is very literal in its interpretation and will catch even the smallest mistake.

You can see how Visual Basic represents object properties in code: the name of the object (that is, its Name property), followed by a period and the property name. The equal sign is Basic’s assignment operator. In effect, it says, “Make the thing on the left (in this case, the ForeColor property of the Text1 object) equal to the thing on the right (the optBlack object’s ForeColor property).”

We use the same technique for the other two Option Buttons. The code for the optBlue button Click event procedure is shown here:

Private Sub optBlue_Click()

Text1.ForeColor = optBlue.ForeColor

End Sub

And here is the code for the Click event procedure of the optRed button:

Private Sub optRed_Click()

Text1.ForeColor = optRed.ForeColor

End Sub

The last steps in creating this project are writing the event procedures for the other two Command Buttons. Each time the user clicks on the Bigger button, we want to increase the size of the Text Box by 20 percent. The size of a control is determined by its Width and Height properties. To increase the width by 20 percent, we would perform the following steps:

1.  Obtain the current width from the control’s Width property.
2.  Multiply the value by 1.2.
3.  Set the Width property equal to the new value.

In code, we can accomplish all three steps with a single Basic statement:

Text1.Width = Text1.Width * 1.2

The asterisk is Basic’s multiplication operator, and the equal sign is the assignment operator we discussed earlier. We also want to increase the height, which we can do by using the same formula. Thus, the complete event procedure for the cmdBigger button looks like this:

Private Sub cmdBigger_Click()

Text1.Width = Text1.Width * 1.2
Text1.Height = Text1.Height * 1.2

End Sub

The final step for our project is to create the event procedure for the Smaller button. We’ll use the same approach as for the Bigger button, except this time we need a multiplication factor of 0.8 to decrease the control size by 20 percent. Here’s the completed event procedure:

Private Sub cmdSmaller_Click()

Text1.Width = Text1.Width * 0.8
Text1.Height = Text1.Height * 0.8

End Sub

That’s it! The project is complete. Let’s take it for a spin. Press F5 to execute the project, and you’ll see the dialog box shown in Figure 3.10. Click on the Option Buttons to change the color of the text in the Text Box. Click on the Bigger and Smaller Command Buttons to resize the Text Box. You’ll see, by the way, that the size of the Text Box is not constrained by the design grid during program execution. This may not be the most exciting program in the world, but I think it’s an effective demonstration of the use of properties in Visual Basic.


Figure 3.10  The Properties Demo program in action, after enlarging the Text Box a few steps.

Note that you can click on the Text Box control and edit the text there. How can this be? We didn’t program any editing capabilities. Actually, you are just seeing the built-in capabilities of the Text Box control.

Here’s another example of the built-in power of Visual Basic controls: If you click on the Smaller button several times, you will see that the Text Box height reaches a certain size and then does not change (although the width will continue to shrink). A Text Box always maintains at least the minimum height necessary to display text. This height will depend on the size of the font in use, which you set in the Font property.

Summing Up

The project in this chapter is fairly simple, but it illustrates some important Visual Basic concepts. You’ve seen how an object’s properties control its appearance and behavior, and how these properties can be manipulated both during program design and by code during program execution. We’ve touched on only a few object properties—it’s obvious from browsing through the Properties list that there are lots more. We’ll be visiting many of them in the following chapters, but it’s hardly possible to cover them all.

If you highlight a property name in the list and press F1, the Visual Basic Help system will display a page of information about the property. A typical Help screen is shown in Figure 3.11. Click on the underlined keywords, or links, to view related information. Among the useful features that the Help system provides are examples of how various properties are used (click on the Example link), links to related topics (click on the See Also link), and a list of the objects that this property applies to (click on the Applies To link). Click on a term with a dotted underline to see a definition of the term. The left side of the Help window lets you navigate among all the available Help topics.


Figure 3.11  Using Visual Basic’s Help system to obtain information about a property.

Perhaps the best way to explore Visual Basic’s properties is to experiment on your own. Whipping up a small project is so easy that experimenting with various property settings should be a regular part of your Visual Basic explorations. Often, seeing something in action is much more effective than simply reading about it. Remember, to learn by doing is our approach.


Previous Table of Contents Next


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

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.