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


Moving And Sizing Controls From Code

Sometimes it’s necessary to move or resize the controls in a form as a program is running, but for some reason, many Visual Basic programmers think you can only do that at design time. In fact, you can do it at runtime easily.

All controls have these properties available at design time or runtime to set their location and dimensions:

  Top—The y coordinate of the top left of the control.
  Left—The x coordinate of the top left of the control.
  Width—The width of the control.
  Height—The height of the control.

You can change all these settings interactively to move or resize a control in a form. Note that all measurements are in twips (1/1440 of an inch) by default, and that the origin (0, 0) in a form is at upper left.

You can also use a control’s Move() method to move a control to a new location:

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

Here’s an example—in this case, when the user clicks a button, Command1, we double the button’s width and height, and move it 500 twips to the left:

Private Sub Command1_Click()
    Const intIncrement = 500
    Command1.Width = 2 * Command1.Width
    Command1.Height = 2 * Command1.Height
    Command1.Move (Command1.Left + intIncrement)
End Sub


TIP:  One way of creating simple animation is to use an Image control to display an image and use its Move() method to move it around a form.

Showing And Hiding Controls In A Form

The Testing Department is on the phone again—does your program really need 120 buttons in the main form? After all, that’s exactly what menus were designed for: to hide controls not needed, getting them out of the user’s way. (In fact, that’s usually a good way to determine if a control item should be in a menu or on the main form: you use menus to make options available to the user at all times, while keeping them out of the way.)

However, let’s say you really don’t want to put your control items into menus—you can still use buttons if you hide the ones that don’t apply at a particular time, showing them when appropriate. Hiding and showing controls in a form as needed can produce dramatic effects at times.

Showing and hiding controls is easy: just use the control’s Visible property. Setting this property to True displays the control; setting it to False hides it. Here’s an example where we make a button disappear (probably much to the user’s surprise) when the user clicks it:

Private Sub Command1_Click()
    Command1.Visible = False
End Sub

Measurements In Forms

The default measurement units for forms are twips, but the project design board says they want the data-entry forms you’re designing to look like real 3×5 cards on the screen. Can you convert from twips to inches in Visual Basic? Yes, you can, and we’ll take a look at that and other measurement issues here.

You can get the dimensions of a form’s client area with these properties:

  ScaleWidth—The width of the client area.
  ScaleHeight—The height of the client area.
  ScaleLeft—The horizontal coordinate of upper left of client area.
  ScaleTop—The vertical coordinate of upper left of client area.

And you can get the overall dimensions of the form using these properties:

  Width—The width of the form.
  Height—The height of the form.
  Left—The horizontal coordinate of upper left of the form
  Top—The vertical coordinate of upper left of the form

You can also use the ScaleMode property to set a form’s coordinate system units—you don’t have to use twips. Here are the possible values for ScaleMode :

  0—User-defined
  1—Twips (1/1440ths of an inch)
  2—Points (1/72nds of an inch)
  3—Pixels
  4—Characters (120 twips horizontally, 240 twips vertically)
  5—Inches
  6—Millimeters
  7—Centimeters

User-Defined Coordinates

To make life easier for yourself, you can set up a user-defined coordinate system: just set the ScaleWidth and ScaleHeight properties yourself. For example, if you want to plot data on a 1000x1000 grid, just set ScaleWidth and ScaleHeight to 1000. To draw a scatter plot of your data, then, you could use PSet() to set individual pixels directly. If one of the points to graph was (233, 599), you could draw that dot this way: PSet(233, 599).


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.