|
|
![]() | |
|
|
|
To access the contents, click the chapter and section titles.
Visual Basic 6 Black Book
Grouping Controls In A FrameThe Testing Department is calling again. Using option buttons to let users specify months and days of the week is OK, but why can they only click one option button at a time? Shouldnt they be able to specify both day of the week and the month? You can make option buttons function together in separate groups. Unless you set up a frame (or picture box) to hold the option buttons, however, theyll all be on the form, which means theyll be in the same group.
To group controls like option buttons, first draw the frame control, and then draw the controls inside the frame.
We already developed a good example of grouping controls in our chapter on option buttons and checkboxes, and well review it here. In that example, we created a tour package program that lets users select from one of four tour packages. When they clicked one of the four option buttons representing each of the four packages, a series of checkboxes in another frame are checked to indicate what cities are in that tour package, as shown in Figure 14.6.
As you can see in Figure 14.6, weve grouped the controls into two frames that have the captions Tour and Cities. The option buttons and checkboxes each function as a control group; when the user selects a tour package by clicking an option button, the program displays the cities in that tour in the checkboxes. Because the option buttons function as a group, only one option button may be selected at a time. The code for this example is located in the tourpackage folder on this books accompanying CD-ROM. Adding A Label To A ProgramThe Testing Department is calling again. What are all those text boxes in your new program, SuperDuperDataCrunch? You explain patiently that they are there to help users with financial planning and let them enter current value, interest rate, time period, taxable base, and so on. Well, they say, you better label those text boxes so users know what they are. Hmm, you think, label them? You can label controls without a Caption property, like text boxes, with the label control. You simply use the Label Control tool in the toolbox to add a label to your form and set its Caption property to display the label you want. You can size the label as desired at design time using the sizing handles that appear around a label when you select it, or at runtime using its Top, Left, Width, and Height properties. As an example, take a look at Figure 14.7. There, weve used labels to describe what value each of six text boxes are supposed to hold. In this way, labels can make your program a great deal easier to work with.
Using Labels Instead Of Text BoxesThere are several advantages to using labels instead of text boxes in a Visual Basic program. Labels display read-only text (although you can make text boxes read-only by setting their Locked property to True), and they give the appearance of text directly on the form, which can look much better than a text box on occasion. Lets see an example. In the stopwatch program we created in our chapter on timers, we used a label to display elapsed time. When the user clicked one button, we set a form-wide variable, StartTime, to the current time using the Now function, and we enabled a timer, Timer1:
Private Sub Command1_Click()
StartTime = Now
Timer1.Enabled = True
End Sub
When the user clicks another button, we stop the stopwatch by disabling the timer:
Private Sub Command2_Click()
Timer1.Enabled = False
End Sub
In the Timer1_Timer() subroutine, which is called by the timer every second, we display the elapsed time in a label named Display:
Sub Timer1_Timer()
Display.Caption = Format$(Now - StartTime, "hh:mm:ss")
End Sub
You might think it odd to display time in a label, but we set the labels font size to 48 point (and its font to Courier New), which makes a very satisfactory display, as you can see in Figure 14.8.
In this way, weve used a label to display text instead of a text box, because the user cant edit the text in the label, and in this case the label looks like a more integral part of the program than a text box would. The code for this example is located in the stopwatch folder on this books accompanying CD-ROM. Formatting Text In LabelsWhen you add labels to a form, you can make the label match the texts size or wrap as needed by setting these label control properties:
In addition, you can format the text in a label with these properties, making the text appear in any font or font size, or with attributes like bold or underline:
Keep in mind that you can use labels as a read-only text box, so formatting the text can be a very useful thing to do.
|
|
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. |