![]() ![]() | ||
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 co-workers think it's hilarious, and they love it. Your boss hates it and asks to see you to discuss time management—immediately.
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, and 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 downward, and positive y values increase to the right.
Here's an example: Say that you wanted to change the size and location of a button in the form when the user clicks that 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 Button1.Size = New Size(100, 50) Button1.Location = New Point(0, 0) End Sub
You also can use the SetBounds method to do the same thing; this method is overloaded and has several forms—here's a popular one:
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.
![]() ![]() | ||