Tutorial - User Interface - Dialog Box/Forms
Posted: Tue Jan 15, 2002 5:11 pm
One of the greatest benefits of VBA is its ability to easily interface with the user.
Dialog boxes or Forms as they are called in VBA provide a simple user interface that can handle many tasks.
Some of these tasks go beyond just user selections (such as radio buttons and check boxes) and can provide error trapping, visual indicators/information that enhance the users understanding of the program.
Behind all controls placed on a form there are a series of actions that can be called when the user selects the item (called Events in VBA). An event is some action that has occurred and can take many forms such as a mouse click or double click or even a selection of another control.
Try this out:
With a new drawing open type VBA at the command prompt to enter the VBA IDE.
Insert a UserForm into the drawing by selecting the InsertUserform from the pull-down menu.
Drag two Command Buttons onto the userform and VBA will name these CommandButton1 and CommandButton2 .
Double click on the CommandButton1 to open the code window of the form and you should see:
Private Sub CommandButton1_Click()
End Sub
There are two pulldown boxes at the top of the code window – one reads CommandButton1 and the other Click. Yes you guessed it - it is the Click Event.
Select the Click pulldown box arrow to display a list of other Events that you can select. Select DblClick and the following code will appear:
Private Sub CommandButton1_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
End Sub
Delete the Sub CommandButton1_Click() Event because we will be using the Double Click Event.
In the Sub CommandButton1_DblClick Event type in:
Code Starts Here
If CommandButton2.Visible = True Then
CommandButton2.Visible = False
Else
CommandButton2.Visible = True
End If
Code Ends Here
Now select the Userform and Run it.
Click on CommandButton1 – Nothing Happens
Double Click on CommandButton1 – CommandButton2 disappears.
Double Click on CommandButton1 again – CommandButton2 appears.
The code we added just makes CommandButton2.Visible either True or False.
Armed with these Events we can display all the controls we want based on user selections – say we have two options, One Circular and the other Rectangular. When the user selects Circular we make visible a TextBox and Label Control on the form and if the user selects Rectangular we can make visible two TextBox and Label Controls on our Form. This saves us room around the drawing environment because we only need one toolbar button to drive both Circular and Rectangular programs.
Next I will create a project for editing text efficiently.
------------------
Regards
John Finlay
Don't want to post a question - email me direct on john@acecad.com.au
Dialog boxes or Forms as they are called in VBA provide a simple user interface that can handle many tasks.
Some of these tasks go beyond just user selections (such as radio buttons and check boxes) and can provide error trapping, visual indicators/information that enhance the users understanding of the program.
Behind all controls placed on a form there are a series of actions that can be called when the user selects the item (called Events in VBA). An event is some action that has occurred and can take many forms such as a mouse click or double click or even a selection of another control.
Try this out:
With a new drawing open type VBA at the command prompt to enter the VBA IDE.
Insert a UserForm into the drawing by selecting the InsertUserform from the pull-down menu.
Drag two Command Buttons onto the userform and VBA will name these CommandButton1 and CommandButton2 .
Double click on the CommandButton1 to open the code window of the form and you should see:
Private Sub CommandButton1_Click()
End Sub
There are two pulldown boxes at the top of the code window – one reads CommandButton1 and the other Click. Yes you guessed it - it is the Click Event.
Select the Click pulldown box arrow to display a list of other Events that you can select. Select DblClick and the following code will appear:
Private Sub CommandButton1_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
End Sub
Delete the Sub CommandButton1_Click() Event because we will be using the Double Click Event.
In the Sub CommandButton1_DblClick Event type in:
Code Starts Here
If CommandButton2.Visible = True Then
CommandButton2.Visible = False
Else
CommandButton2.Visible = True
End If
Code Ends Here
Now select the Userform and Run it.
Click on CommandButton1 – Nothing Happens
Double Click on CommandButton1 – CommandButton2 disappears.
Double Click on CommandButton1 again – CommandButton2 appears.
The code we added just makes CommandButton2.Visible either True or False.
Armed with these Events we can display all the controls we want based on user selections – say we have two options, One Circular and the other Rectangular. When the user selects Circular we make visible a TextBox and Label Control on the form and if the user selects Rectangular we can make visible two TextBox and Label Controls on our Form. This saves us room around the drawing environment because we only need one toolbar button to drive both Circular and Rectangular programs.
Next I will create a project for editing text efficiently.
------------------
Regards
John Finlay
Don't want to post a question - email me direct on john@acecad.com.au

