|
|
![]() | |
|
|
|
To access the contents, click the chapter and section titles.
Visual Basic 6 Black Book
Resizing And Moving Buttons From CodeYour new April Fools program has an Exit button, but it moves around and resizes itself, making it a moving target for the user to try to hit. Your coworkers think its hilarious and they love it. Your boss hates it and asks to see you in his cubicle to discuss time managementimmediately. How do you move buttons and resize them in code? You use the Top, Left, Height, and Width properties, or the Move method. Heres what those properties hold:
(When setting these properties, remember that the default measurement units in Visual Basic are twips, and that the default coordinate systems origin is at upper left in a form.) And heres how you use the Move method: Button.Move left, [ top, [ width, [ height ]]] Lets see an example; here, we move a command button 500 twips to the right when the user clicks it:
Private Sub Command1_Click()
Const iIncrement = 500
Command1.Move Command1.Left + iIncrement
End Sub
Adding A Picture To A ButtonYour boss (whos been angling for a promotion) wants the company logo to appear in all the buttons in your program. Before you start looking for a new job, take a look at the Visual Basic Picture property. Using the Picture property, you can load an image into a buttonjust click the button with the ellipsis ( ) in the Picture propertys entry in the Properties window and indicate an image file in the Load Picture dialog box that opens. Thats not all, howeveryou also have to set the buttons Style property to Graphical (which has a numeric value of 1). Weve loaded an image into a command button in Figure 7.10.
When you set checkboxes and option buttons to graphical style, they actually look just like graphical command buttons. The only difference is that when you click a graphical checkbox or option button, as shown in Figure 7.11, they stay clicked until you click them again (and option buttons still function in groups, of course).
You can also set the Picture property at runtimebut dont try setting it directly to the name of a file. You can only load Visual Basic Picture objects into the Picture property; such objects are returned by the LoadPicture() function like this:
Private Sub Command1_Click()
Command1.Picture = LoadPicture("c:\vbbb\picturebuttons\image.bmp")
End Sub
Adding A Down Picture To A ButtonBesides adding a simple image to a button, you can add an image that is displayed when the button is down. This is more useful with checkboxes and option buttonswhich stay down when clickedthan it is with command buttons. Using the DownPicture property, you can load an image into a buttonjust click the button with the ellipsis ( ) in the DownPicture propertys entry in the Properties window, and indicate an image file in the Load Picture dialog box that opens. You also have to set the buttons Style property to Graphical (which has a numeric value of 1). For example, weve loaded a down image into a command button in Figure 7.12.
You can also set the DownPicture property at runtime using the LoadPicture() function:
Private Sub Check1_Click()
Check1.DownPicture = LoadPicture("c:\vbbb\picturebuttons\image2.bmp")
End Sub
Adding Buttons At RuntimeYour new program lets the user add options to customize things, and you want to display a new button for each option. Is there a way to add buttons to a Visual Basic program at runtime? Yes, there is. You can use the Load statement to load new buttons if theyre part of a control array. To see how this works, add a new button to a form, giving it the name, say, Command. To make it the first member of a control array, set its Index property to 0. Now when the user clicks this button, we can add a new button of the same type to the form with Load. Here, we load Command(1), because Command(0) is already on the form:
Private Sub Command_Click(Index As Integer)
Load Command(1)
End Sub
The new button is a copy of the original onewhich includes the original buttons positionso we move the new button so it doesnt cover the original one:
Private Sub Command_Click(Index As Integer)
Load Command(1)
Command(1).Move 0, 0
End Sub
Finally, we make the new button visible by setting its Visible property to True:
Private Sub Command_Click(Index As Integer)
Load Command(1)
Command(1).Move 0, 0
Command(1).Visible = True
End Sub
And thats itweve added a new button to the program at runtime.
Passing Buttons To ProceduresYouve got 200 buttons in your new program, and each one has to be initialized with a long series of code statements. Is there some easy way to organize this process? There is. You can pass the buttons to a procedure and place the initialization code in that procedure. Heres an example. We can set a buttons caption by passing it to a subroutine named SetCaption() like this:
Private Sub Command1_Click()
SetCaption Command1
End Sub
In the SetCaption() procedure, you just declare the button as a parameter; well name that parameter Button and make it of type Control: Private Sub SetCaption(Button As Control) End Sub Now we can refer to the passed button as we would any parameter passed to a procedure, like this:
Private Sub SetCaption(Button As Control)
Button.Caption = "You clicked me!"
End Sub
The result appears in Figure 7.13when you click the command button, the SetCaption() subroutine changes its caption, as shown.
|
|
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. |