To create a form, on the main menu of Microsoft Visual Basic,
you can click Insert -> UserForm. This would add a form to your
project. In the same way, you can add as many forms as you want.
The form is primarily used as a platform on which you
add other controls. For this reason, a form is referred to as a container.
By itself, a form is not particularly useful. You should add other objects
to it.
When you create or add a form, a module is also
automatically created for it. To access the module associated with a form,
you can right-click the form and click View Code.
Although you create a form in Microsoft Visual Basic,
you view its results in Microsoft Excel. You have various options.
A form is referred to as modal if the user cannot
access anything from the same application while the form is displaying. A
form is called modeless if the user can click something of the same
application behind that form while the form is displaying.
To display the run-time view of a form in modal view:
You can also programmatically display a form. To
support this, the UserForm object is equipped with a method named Show.
Its syntax is:
Public Sub UserForm.Show(Optional ByVal Modal As Boolean)
This method takes one Boolean argument that is
optional. If you omit it, the form would display as modal and the user
cannot do anything else in Microsoft Excel as long as the form is not
closed. That's the default behavior. If you want to display the form as
modeless, pass the argument as False. Here is an example:
Private Sub Exercise()
UserForm1.Show False
End Sub
If you have equipped a form with many aesthetic
objects you want to see on a piece of paper, you can print it. To support
printing, the UserForm object is equipped with a method named PrintForm.
Its syntax is:
Public Sub PrintForm
This method takes no argument. When called, it sends
the (body of the) form directly to
Private Sub Exercise()
UserForm1.PrintForm
End Sub
As opposed to displaying a form, if it is already
showing, you can hide it. To allow you to hide a form, the UserForm object
is equipped with a method named Hide. Its syntax is:
Pyblic Sub UserForm.Hide
This method takes no argument. When called, it hides
the form (without closing it). Here is an example of calling it:
Private Sub Exercise()
UserForm1.Hide
End Sub
After using a form, the user can close it by clicking
the system close button. To programmatically close a form, use the End
statement. Here is an example:
Private Sub Exercise()
End
End Sub
|
|
|||||||||||||||||||||||
|
Like every object on a computer, the primary
characteristic of a form is its name. After creating a form, access its
Properties window, click (Name), type the desired name and press Enter
When a form displays in normal view to the user, it is
usually centered. The user can then drag its title bar to move it around.
You too can move a form.
If you want to specify the position a form would
assume when it displays, at design time, access its Properties window.
Then change the values of the Left and the Top properties. You can also
programmatically control the location of a form. You have two options. You
can assign the desired values to its Left and/or Top
properties. Here is an example:
Private Sub Exercise()
UserForm1.Left = 400
End Sub
Al alternative is to call the Move method. Its
syntax is:
Public Sub UserForm.Move(ByVal Left As Single, ByVal Top As Single, Optional ...)
This method takes two required arguments. The first
represents the left distance and the second is the top distance. Here is
an example:
Private Sub Exercise()
UserForm1.Move 200, 200
End Sub
When you have just added a new form, it assumes a
default size. If that size doesn't fit your intentions, you can change.
To change the size of a form, you can access its
Properties window. Then change the values of the Height and Width. To
programmatically change the size of a form, you have many options. You can
assign the desired values to its Height and/or to its Width. Here is an
example:
Private Sub Exercise()
UserForm1.Width = 600
End Sub
Another option is to call the Move method. In reality,
this method takes two required arguments and two optional arguments. Its
actual syntax is:
Public Sub UserForm.Move(ByVal Left As Single, ByVal Top As Single, _ Optional ByVal Width As Single, Optional ByVal Height As Single)
The last two optional arguments allow you to specify
the size of the form. Here is an example:
Private Sub Exercise()
UserForm1.Move 200, 200, 1040, 600
End Sub
|
No comments:
Post a Comment