Sams Teach Yourself Visual Basic 6 in 24 Hours
- Table of Contents
- Copyright
- About the Author
- Acknowledgments
- Introduction
- Who Should Read This Book
- What This Book Will Do for You
- Can This Book Really Teach Visual Basic in 24 Hours?
- What You Need
- Files on the Visual Basic Distribution CD-ROM
- Conventions Used in This Book
- Enough! Time Is Ticking!
- Part I: Introducing Visual Basic
- Hour 1. Visual Basic at Work
- Hour 2.Analyzing Visual Basic Programs
- Hour 3.Controls and Properties
- Hour 4.Examining Labels, Buttons, and Text Boxes
- Part II: Coding the Details
- Hour 5.Putting Code into Visual Basic
- Hour 6.Message and Input Boxes
- Hour 7.Making Decisions
- Hour 8.Visual Basic Looping
- Part III:Putting Code to Work
- Hour 9.Combining Code and Controls
- Hour 10.List Boxes and Data Lists
- Hour 11.Additional Controls
- Hour 12.Dialog Box Basics
- Part IV:Programming with Data
- Hour 13.Modular Programming
- Hour 14.Built-In Functions Save Time
- Hour 15.Visual Basic Database Basics
- Hour 16.Printing with Visual Basic
- Part V:Sprucing Up Programs
- Hour 17.Menus and Visual Basic
- Hour 18.The Graphic Image Controls
- Hour 19.Toolbars and More Graphics
- Hour 20.Writing Correct Applications
- Part VI:Advancing Visual Basic Applications
- Hour 21.Visual Basic and ActiveX
- Hour 22.Object Basics
- Hour 23.Distributing Your Applications
- Hour 24.Online Visual Basic
- Part VII:Appendixes
- Appendix A.Operator Precedence
- Appendix B.Answers
- Appendix C.Using the CD-ROM
Labels
Labels hold the primary text that appears on a form. Often, programmers use labels to place titles in forms and to label text boxes so that users know what to type into the text box. Visual Basic supports several other ways to put text on forms, but when you use the Label control, your code can subsequently, at runtime, change the label's text so that different messages can appear when needed. Figure 4.3 shows a Form window that contains a label used for the application's title.
Figure 4.3 A label forms the title on this form.
When you place labels on a form, you'll almost always set the Label control's Name property and type a new Caption value. In addition, you'll want to change the Font property and possibly the label's color and style. You will rarely write event procedure code for labels, so a label's overhead is fairly small and the programming effort required to manipulate labels is minimal.
Table 4.2 lists the most common Label control properties that you'll set as you work with the Label control.
Table 4.2. Common label properties.
| Property | Description |
| Alignment | Determines whether the label's caption appears left-justified, centered, or right-justified within the label's boundaries. |
| AutoSize | Enlarges the label's size properties, when True, if you assign a caption that is too large to fit in the current label's boundaries at runtime. |
| BackColor | Specifies the label's background color. Click the BackColor palette's down arrow to see a list of colors and click System to see a list of common Windows control colors. |
| BackStyle | Determines whether the background shows through the label or if the label covers up its background text, graphics, and color. |
| BorderStyle | Determines whether a single-line border appears around the label. |
| Caption | Holds the text that appears on the label. |
| Enabled | Determines whether the label is active. Often, you'll change the Enabled property at runtime with code when a label is no longer needed. |
| Font | Produces a Font dialog box in which you can set the caption's font name, style, and size. |
| ForeColor | Holds the color of the label's text. |
| Height | Holds the height of the label's outline in twips. |
| Left | Holds the number of twips from the label's left edge to the Form window's left edge. |
| MousePointer | Determines the shape of the mouse cursor when the user moves the mouse over the label. |
| TabIndex | Specifies the order of the label in the focus order. Although labels cannot receive focus, they can be part of the focus order. |
| ToolTipText | Holds the text that shows as a ToolTip at runtime. |
| Top | Holds the number of twips from the label's top edge to the Form window's top edge. |
| Visible | Determines whether the label appears or is hidden from the user. |
| Width | Holds the width of the label in twips. |
| WordWrap | Determines whether the label expands to fit whatever text appears in the caption. |
Labels can present problems if they receive text that is too large for the label boundaries. Putting captions in labels seems easy until you think about the effects that can occur if the label is too large or too small to hold the text. By using certain property combinations, you can add automatically adjusting labels for whatever text the labels need to hold.
Suppose that you design a label that contains this long caption:
This label's caption is extremely long-winded, just like the author.
A label is rarely wide enough or tall enough to hold a caption this long. If you attempt to type text into a label's Caption property that is longer than what fits within the label's size properties (Left, Height, Top, and Width), one of the following things can take place, depending on how you've set up the label:
-
The text might not fit inside the label, and Visual Basic truncates the text. Figure 4.4 shows what can happen in this case.
Figure 4.4 The label cannot display the entire caption.
Set the AutoSize property to False if you want the label to remain the same size and not resize automatically to fit the Caption property value. If the code assigns long text, the label might not hold the entire caption, but the label will not expand and get in the way of other controls.
-
The label automatically expands downward to hold the entire caption in a multiline label. Figure 4.5 shows the result.
Figure 4.5 The label resizes downward to hold the entire Caption property.
To expand the label downward when needed to hold the caption, set both the AutoSize and WordWrap properties to True. Subsequently, if the code changes the caption to hold a long line of text, the label will expand to display the entire message.
-
The label automatically expands horizontally across the screen to hold the entire caption in a long label control. Figure 4.6 shows the result.
Figure 4.6 A horizontally resizing label could bump off other controls.
A long label like this isn't necessarily a bad label. Depending on the length of the text you assign to the label during the program's execution, there might be plenty of screen space to display long labels. To automatically expand the label horizontally, set the AutoSize property to True but leave WordWrap set to False.
Text Boxes | Next Section

Account Sign In
View your cart