|
|
|
![]() | |
|
|
|
To access the contents, click the chapter and section titles.
Visual Basic 6 Black Book
Aligning Text In LabelsAs with text boxes, you can align text in labels. To do that, you just set the labels Alignment property at design time or runtime. Here are the possible values for the Alignment property:
For example, if youre writing a calculator program and have a column of right-justified text boxes above a label that displays a running sum, you can also right-justify the label to match the controls above it. Handling Label Control EventsHeres something that even experienced Visual Basic programmers often dont know: labels have events like Click and DblClick (although they dont have any keystroke-handling events). Using these events can be a good thing if youre using a label control as more than just a labelfor example, to reset a setting of some kind. Heres an example using the DblClick event. We developed a stopwatch program in our chapter on timers (Chapter 13) and displayed the elapsed time in a label named Display in that program. To make life easier for users, we can let them just double-click that label to reset the stopwatch to 00:00:00. Doing so is easy; we just add an event handle for the labels DblClick event: Private Sub Display_DblClick() End Sub To reset the stopwatch, we just use the Visual Basic Now function to set the start time, held in a variable named StartTime, to the current time:
Private Sub Display_DblClick()
StartTime = Now
End Sub
And thats it. Now when the user double-clicks the stopwatchs display, the stopwatch is reset to 00:00:00. Weve made effective use of a labels DblClick event. Using Labels To Give Access Keys To Controls Without CaptionsThe Testing Department is calling again. The old thorny issue of keyboard access has come up again. Theoretically, they say, users should be able to use your program, SuperDuperDataCrunch, with just the keyboard. Fine, you say, we can add access keys to all the button captions so the user can give the button the focus just by pressing Alt + the access key (just like menu items). Dont forget to do the same to all the text boxes, the testing department says. You think, how do you give an access key to a text box? This is where a useful aspect of labels comes in handy. In fact, this aspect of the label control is built just to handle this problem. You can give access keys to controls with Caption properties just by placing an ampersand (&) in the caption in front of the letter you want to make the access keybut how can you do that if a control (like a text box) has no Caption property? Heres the way you do it: you give the access key to a label control and then make sure the control you want to give the focus to with that access key is next in the tab order (that is, has the next highest TabIndex property value). Because labels cannot accept the focus themselves, this is a neat feature: when the user presses Alt + the access key, the label passes the focus on to the next control. In this way, you can give even controls without Caption properties access keys.
As an example, weve given the two labels in Figure 14.9 access keys. When the user presses Alt + the access key above a text box, the focus is set to that text box, because those text boxes follow their individual labels in the tab order.
Now were using access keys with text boxes. Adding A Shape Control To A ProgramThe Aesthetic Design Department is calling again. Cant you jazz up the appearance of your program a little? How about something to give it a little pizzazz? Looking around, you happen to notice the shape control. OK, you say, no problem. You use the shape control at design time to draw shapes on a form or picture box. The shapes you can draw are rectangles, squares, ovals, circles, rounded rectangles, and rounded squares. At runtime, you can access and change the shape controls properties like Left, Top, Width, Height, BackColor, FillStyle, or FillColor, and use its methods, like Move or Refresh. However, shape controls have no events, so they cant respond directly to user actions like clicks. You draw a shape using the Shape Control tool, which appears in the Visual Basic toolbox when Visual Basic starts. Just draw the shape as you want it (it starts as a rectangle). To set the shapes type (for example, a rectangle, square, oval, and so on), you set the controls Shape property to one of the following values:
One important use of shape controls is to group other controls together. (Note, however, that shape controls cant act as true control containers in the way picture boxes or frames can. For example, you cant group option buttons together with shapes.) In Figure 14.10, were using shape controls to group the buttons visually into two groups.
You can also set the width of the shapes drawing line with the BorderWidth property and fill the shape using the FillColor and FillStyle properties. The BorderStyle property lets you select the style of the shapes drawing line, including using dots and dashes. For more on this control, see the other topics in this chapter.
|
|
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. |