| 416-621-9348 cgreaves@chrisgreaves.com | |
|---|---|
| Home Site About Services Products |
Programming Command Buttons
In the previous lesson we built a GUI form and on that form we placed some controls; specifically, four controls were placed – a text box, a list box and two command buttons.
In this lesson we will build program code to make the command buttons “work”.
In VBE double-click on the Cancel button. The GUI form will temporarily disappear and an empty procedure will appear in its place:
Private Sub cmdCancel_Click() End Sub
Introduce one line of code into this skeletal procedure – the code says “Unload Me”.
“Me” is a standard name for the current user form; no matter what names or captions we have assigned the user form, within itself we can always refer to the form itself as “Me”.
Private Sub Cancel_Click() Unload Me End Sub
Run the form (with the F5 key) and observe that you can now clear the form away (as a user) by either tapping the Escape key or by clicking the Cancel button.
In VBE double click the OK button:
Private Sub cmdOK_Click() End Sub
We want the OK button to display something useful BEFORE it hides the user form:
Private Sub cmdOK_Click() MsgBox Me.tbInput Me.Hide End Sub
When you run the form, type some text in the text box and then click the OK command button; a small message-box should pop up containing the text you had keyed into the text box.
Of course, if you had typed no text, you’d get a message box with no message!
Private Sub cmdOK_Click() If Me.tbInput <> "" Then MsgBox Me.tbInput Else End If Me.Hide End Sub
In the next lesson we will learn how to initialize a list box, and how to make use of it in our GUI form.
Loading
Toronto and Mississauga, Friday, March 19, 2010 10:54 AM
Copyright © 1996-2010 Chris Greaves. All Rights Reserved.