|
|
|
![]() | |
|
|
|
To access the contents, click the chapter and section titles.
Visual Basic 6 Black Book
Moving And Sizing Controls From CodeSometimes its 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:
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 controls Move() method to move a control to a new location: object.Move left, [top, [width, [height]]] Heres an examplein this case, when the user clicks a button, Command1, we double the buttons 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
Showing And Hiding Controls In A FormThe Testing Department is on the phone againdoes your program really need 120 buttons in the main form? After all, thats exactly what menus were designed for: to hide controls not needed, getting them out of the users way. (In fact, thats 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, lets say you really dont want to put your control items into menusyou can still use buttons if you hide the ones that dont 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 controls Visible property. Setting this property to True displays the control; setting it to False hides it. Heres an example where we make a button disappear (probably much to the users surprise) when the user clicks it:
Private Sub Command1_Click()
Command1.Visible = False
End Sub
Measurements In FormsThe default measurement units for forms are twips, but the project design board says they want the data-entry forms youre 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 well take a look at that and other measurement issues here. You can get the dimensions of a forms client area with these properties:
And you can get the overall dimensions of the form using these properties:
You can also use the ScaleMode property to set a forms coordinate system unitsyou dont have to use twips. Here are the possible values for ScaleMode :
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).
|
|
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. |