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 Black Book
(Publisher: The Coriolis Group)
Author(s): Steven Holzner
ISBN: 1576102831
Publication Date: 08/01/98

Bookmark It

Search this book:
 
Previous Table of Contents Next


Grouping Controls In A Frame

The 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? Shouldn’t 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, they’ll all be on the form, which means they’ll be in the same group.


WARNING!  If you draw a control outside the frame and then try to move it inside, the control will be on top of the frame, not in it, which means the control will not be grouped with other controls in the frame.

To group controls like option buttons, first draw the frame control, and then draw the controls inside the frame.


TIP:  At design time, you can also align the controls in a frame. Just select multiple controls by holding down the Ctrl key, and use the Format menu to align the controls or set their spacing uniformly.

We already developed a good example of grouping controls in our chapter on option buttons and checkboxes, and we’ll 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.


Figure 14.6  Grouping controls using frames.

As you can see in Figure 14.6, we’ve 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 book’s accompanying CD-ROM.

Adding A Label To A Program

The 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, we’ve 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.


Figure 14.7  Labeling text boxes with label controls.


WARNING!  Don’t forget that you can set labels’ captions at runtime simply by changing their Caption property. In other words, labels can display text just like text boxes can, except that you can’t edit it, and the text appears as though it’s directly on the form. For more on this, see the next topic.

Using Labels Instead Of Text Boxes

There 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.

Let’s 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 label’s 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.


Figure 14.8  Displaying time in a label control.

In this way, we’ve used a label to display text instead of a text box, because the user can’t 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 book’s accompanying CD-ROM.

Formatting Text In Labels

When you add labels to a form, you can make the label match the text’s size or wrap as needed by setting these label control properties:

  AutoSize makes the label size itself to fit the text.
  WordWrap enables word wrap if lines of text are too long.

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:

  FontBold
  FontItalic
  FontName
  FontStrikeThru
  FontUnderline

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.


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.