![]() |
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
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!
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 Buttons ForeColor property. All we need to do is copy that number from the Option Buttons ForeColor property to the Text Boxs ForeColor property, and we accomplish our task without having to worry about the exact color number. Heres the event procedure with the single necessary line of code added: Private Sub optBlack_Click() Text1.ForeColor = optBlack.ForeColor End Sub
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 Basics 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 objects 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:
In code, we can accomplish all three steps with a single Basic statement: Text1.Width = Text1.Width * 1.2 The asterisk is Basics 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. Well 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. Heres the completed event procedure: Private Sub cmdSmaller_Click() Text1.Width = Text1.Width * 0.8 Text1.Height = Text1.Height * 0.8 End Sub Thats it! The project is complete. Lets take it for a spin. Press F5 to execute the project, and youll 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. Youll 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 its an effective demonstration of the use of properties in Visual Basic.
Note that you can click on the Text Box control and edit the text there. How can this be? We didnt program any editing capabilities. Actually, you are just seeing the built-in capabilities of the Text Box control. Heres 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 UpThe project in this chapter is fairly simple, but it illustrates some important Visual Basic concepts. Youve seen how an objects properties control its appearance and behavior, and how these properties can be manipulated both during program design and by code during program execution. Weve touched on only a few object propertiesits obvious from browsing through the Properties list that there are lots more. Well be visiting many of them in the following chapters, but its 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.
Perhaps the best way to explore Visual Basics 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.
|
![]() |
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. |