|
|
Title | Write a subroutine in a code module that scales controls without moving them in Visual Basic .NET |
Description | This example shows how to write a subroutine in a code module that scales controls without moving them in Visual Basic .NET. |
Keywords | subroutine, code module, module, scale control, resize control, stretch control, Visual Basic .NET, VB.NET |
Categories | Controls, Software Engineering |
|
|
A code module is a file where you can place routines, variables, and other pieces of code that you don't want associated with anym class. To create a code module, open the Project menu and select Add Module. Give the module a name and click OK.
In this example, the module contains the following subroutine, which scales a control while keeping it centered in its original location. (This is really an example of using a module. I'm not sure if you'll ever want to scale a control like this.)
|
|
' Resize the control keeping it centered.
Public Sub StretchControl(ByVal ctl As Control, ByVal scale _
As Single)
Dim new_width As Single = ctl.Width * scale
Dim new_height As Single = ctl.Height * scale
Dim cx As Double = ctl.Left + ctl.Width / 2
Dim cy As Double = ctl.Top + ctl.Height / 2
ctl.Bounds = New Rectangle( _
CInt(cx - new_width / 2), _
CInt(cy - new_height / 2), _
CInt(new_width), _
CInt(new_height))
End Sub
|
|
The code calculates the control's new size. It then figures out where it should position the control to keep it centered in the same place. It finsihes by setting the control's Bounds property to set the size and position in a single step.
|
|
|
|
|
|
|
|
|