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


Resizing And Moving Buttons From Code

Your new April Fool’s 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 it’s hilarious and they love it. Your boss hates it and asks to see you in his cubicle to discuss time management—immediately.

How do you move buttons and resize them in code? You use the Top, Left, Height, and Width properties, or the Move method. Here’s what those properties hold:

  Left holds the horizontal coordinate of the upper left of the button.
  Top holds the vertical coordinate of the upper left of the button.
  Height holds the button’s height.
  Width holds the button’s width.

(When setting these properties, remember that the default measurement units in Visual Basic are twips, and that the default coordinate system’s origin is at upper left in a form.)

And here’s how you use the Move method:

Button.Move left, [ top, [ width, [ height ]]]

Let’s 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 Button

Your boss (who’s 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 button—just click the button with the ellipsis (…) in the Picture property’s entry in the Properties window and indicate an image file in the Load Picture dialog box that opens. That’s not all, however—you also have to set the button’s Style property to Graphical (which has a numeric value of 1). We’ve loaded an image into a command button in Figure 7.10.


Figure 7.10  Adding a picture to a button.

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


Figure 7.11  A graphical checkbox.

You can also set the Picture property at runtime—but don’t 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 Button

Besides 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 buttons—which stay down when clicked—than it is with command buttons.

Using the DownPicture property, you can load an image into a button—just click the button with the ellipsis (…) in the DownPicture property’s entry in the Properties window, and indicate an image file in the Load Picture dialog box that opens.

You also have to set the button’s Style property to Graphical (which has a numeric value of 1). For example, we’ve loaded a down image into a command button in Figure 7.12.


Figure 7.12  Adding a down picture to a graphical checkbox.

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


TIP:  You can also add an image to be displayed in a graphical button when it’s disabled by using the DisabledPicture property.

Adding Buttons At Runtime

Your 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 they’re 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 one—which includes the original button’s position—so we move the new button so it doesn’t 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 that’s it—we’ve added a new button to the program at runtime.


TIP:  You can also remove buttons at runtime by unloading them with Unload.

Passing Buttons To Procedures

You’ve 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.

Here’s an example. We can set a button’s 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; we’ll 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.13—when you click the command button, the SetCaption() subroutine changes its caption, as shown.


Figure 7.13  Passing a button to a procedure to change its caption.


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.