JavaScript Editor JavaScript Editor     JavaScript Debugger

Previous Section Next Section

Main Page

Moving and Sizing Forms and Controls in Code

In Visual Basic 6.0 and earlier, you could use the Move method to move forms and controls (and optionally set their dimensions), and the Height and Width methods to set their dimensions. In VB .NET, you use the SetBounds method to move forms and controls (and optionally set their dimensions), and the Size and Location properties to set their dimensions.

You set the Size property to a new Size object, and the Location property to a new Point object. The dimensions you set in Size and Point objects are measured in pixels, as are all measurements in Visual Basic; you create these objects by passing x and y dimensions to their class's constructors, like this: Size(x_dimension, y_dimension) and Point(x_location, y_location). (Note that in the Visual Basic screen coordinate system, the upper left of the screen is the origin (0, 0) and that positive x values increase downwards, and positive y values increase to the right.)

Here's an example. Suppose you wanted to change the size and location of both a form and a button in the form when the user clicks a button. You can do that like this (the origin of coordinates for the form is the upper left of the screen and the origin for the button contained in the form is the upper left of the form's client area):

Private Sub Button1_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles Button1.Click
        Size = New Size(100, 100)
        Location = New Point(0, 0)
        Button1.Size = New Size(100, 50)
        Button1.Location = New Point(0, 0)
End Sub

You can also use the SetBounds method to do the same thing:

Overloads Public Sub SetBounds(ByVal x As Integer, ByVal y As Integer, _
   ByVal width As Integer, ByVal height As Integer)

Here are the arguments you pass to SetBounds:

  • x—The new Left property value of the control.

  • y—The new Right property value of the control.

  • width—The new Width property value of the control.

  • height—The new Height property value of the control.

As you see with the Overloads keyword above, SetBounds is overloaded, which means there is more than one form of this method. Here is the other form (Visual Basic will know which one you want to use depending on how many arguments you pass):

Overloads Public Sub SetBounds(ByVal x As Integer, ByVal y As Integer, _
   ByVal width As Integer, ByVal height As Integer, _
   ByVal specified As BoundsSpecified)

This form has a new argument, specified, which is a combination of values from the BoundsSpecified enumeration; here are the possible values (to use the X item, use BoundsSpecified.X, and so on):

  • All— Specifies that both Location and Size property values are indicated.

  • Height— Specifies that the height of the control is indicated.

  • Location— Specifies that both x and y coordinates of the control are indicated.

  • None— Specifies that no bounds are indicated.

  • Size— Specifies that both Width and Height property values of the control are indicated.

  • Width— Specifies that the width of the control is indicated.

  • X— Specifies that the left edge of the control is indicated.

  • Y— Specifies that the top edge of the control is indicated.

To use SetBounds to do the same thing as in the code at the beginning of this topic, you can do this:

Private Sub Button1_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles Button1.Click
        SetBounds(0, 0, 100, 100)
        Button1.SetBounds(0, 0, 100, 50)
End Sub
Tip 

One way of creating simple animation is to use a picture box control to display an image and use the SetBounds method to move it around a form.

Previous Section Next Section




JavaScript Editor Free JavaScript Editor     JavaScript Editor