Userforms.
If you have programmed with AutoLisp and the Dialogue Control Language
(DCL), you will be familiar with designing dialogue boxes. Compared to DCL
though, designing dialogue boxes in VBA is a breeze. The basis of all
dialogue boxes in VBA is the Userform. This Tutorial will show you how to
display and manipulate Userforms.
A Userform is a container that holds all the controls such as labels,
textboxes,
pictures, etc. that make up part of your applications interface.
A Userform has it's own Properties, Methods and Events. Let's have a look
at some of them :
Displaying a Userform
The syntax for displaying a Userform is as follows :
UserFormName.Show
So, to display a Userform named Userform1, you would use the following
code:
Userform1.Show
If you want to, you can preload the Userform into memory without
actually displaying it. This can be useful as it can sometimes take a few
seconds for a complex Userform to appear. The ability to preload the
Userform allows you to decide when you would like this operation to take
place. To preload a Userform you would use the following code :
Load Userform1
Hiding/Unloading a Userform
To temporarily hide a Userform, you would use the Hide method. This is
a very good example of how dialogue boxes in VBA are so much simpler than
in AutoLisp. To hide a Userform you would use the following code:
Userform1.Hide
To Unload a Userform from memory use the following code :
Unload Userform1
You could also use the 'Me' keyword :
Unload Me
Useform Events
Userforms support many predefined events. Among the most commonly used
events are Initiliaze, Click and Terminate events.
Note A VBA module that contains an event procedure can be called
a module
'behind' the Userform. A module that contains event procedures is not
visible in the Modules collection of the Projects window of the VBA
Editor. You must double-click the body of the Userform to view the
Userform Code Module.
Let's have a look at some Userform Events. Start your VBA Editor and
insert a
Userform into a new Project. Double-Click the Userform and type in the
following code:
Private Sub UserForm_Click()
Me.Height = Int(Rnd * 500)
Me.Width = Int(Rnd * 750)
End Sub
Private Sub UserForm_Initialize()
Me.Caption = "Userform Events"
Me.BackColor = RGB(10, 25, 100)
End Sub
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
msg = "Now Unloading " & Me.Caption
MsgBox prompt:=msg, Title:="QueryClose Event"
End Sub
Private Sub UserForm_Resize()
msg = "Width: " & Me.Width & Chr(10) & "Height : " & Me.Height
MsgBox prompt:=msg, Title:="Resizing Event"
End Sub
Private Sub UserForm_Terminate()
msg = "Now Unloading " & Me.Caption
MsgBox prompt:=msg, Title:="Terminate Event"
End Sub
Now run the Userform.
This is what happens when you run the project:
Firstly, The Intialize event procedure changes the Caption property to
"Userform Events" and the Backcolor property to dark Blue.
When you click the Userform the Click Event procedure is initiated and
the Userform is re-sized. Also, because you created a resize event
procedure, you receive 2 message boxes. The resize event occurs twice
because your code behind the click event changed both the Width and Height
properties of the Userform.
When you close the Userform, the QueryClose event procedure is
triggered. This displays a message box with the caption you gave the
Userform in the code for the Initialize event. The QueryClose event is
useful when you want to perform a certain set of actions when the Userform
is closed by the user.
The Terminate Event then triggers a message box which states that the
Caption of the Userform is Userform1. The Terminate Event occurs after the
Userform is removed from memory and the Caption of the Userform returns to
it's original state. |