master VBA dialog boxes1 Oct, 2003 By: Barry Bowen
Drag and drop to build dialog boxes to gather data for your VBA routines.
One time-saving feature of VBA (Visual BASIC for Applications) is its ability to create forms and dialog boxes to gather data for your programs. Using AutoCAD, we'll look at how to code VBA controls for user-defined forms or dialog boxes by focusing on a specific control in each of the next few installments. This is a fast-track approach. Many VBA reference books are available, so we'll skip the basic details and get right to implementing a VBA project with a user interface. Although this column is designed as a starting point for newcomers to VBA, you should at least be familiar with the VBA IDE (integrated development environment) components for placing controls on a form and changing their properties.
LET'S GET STARTED
To open AutoCAD's VBA IDE, select Tools | Macro | Visual Basic Editor. In the VB editor, select Insert | User Form to insert a blank form. Rename the form caption in the Properties window to Line1 Program.
Figure 1. TextBox control lets you enter a single line of text as input to a VBA routine.
CREATE THE USER INTERFACE
Click the Textbox control in the toolbox and move your cursor over the form to place the distance input text box. Change the control name to Distance.
Click the TextBox control again to place the angle input box below the distance box. Change the control name to Angle.
Get the CodeDownload the code for this and all articles in our Get the Code area. Look for file oct03.exe. You will need to register at the site (no charge).
Click the CommandButton control to add the OK and Cancel buttons as shown. Change the command button control's name to OK and Cancel and change the captions to reflect each button's use.
WRITING THE CODE
Now that you've created a dialog box, you're ready to write the code for the Line1 program. A TextBox gathers information from the program's user. The text label displays the type of information required. Command buttons execute the program or cancel and close the dialog box.
Double-click on the Cancel command button, and the code window appears, as shown in figure 2.
Figure 2. VBA code window shows all events for all controls on a form.
The code window shows all of the events for all the controls on the form. Type End in the Cancel_Click event for the Cancel button.
Double-click UserForm1 in the Project window to return to the form. Double-click the OK command button and add the following code in the "OK_Click" event in the code window:
Private Sub Cancel_Click() End End Sub
THE LINE1 PROGRAM
Private Sub OK_Click() 'Hide the dialog for user input Me.Hide 'Define the variables Dim StartPoint As Variant Dim EndPoint As Variant Dim myLine As AcadLine Dim dblAngle As Double 'convert Angle string from textBox into radians dblAngle = ThisDrawing.Utility.AngleToReal(Angle, acDegreeMinuteSeconds) 'Prompt user for line starting point StartPoint = ThisDrawing.Utility.GetPoint(, vbCr & "Line Start Point: ") 'Find the end point of the line based on Distance and Angle EndPoint = ThisDrawing.Utility.PolarPoint (StartPoint, dblAngle, Distance) 'Create the line object Set myLine = ThisDrawing.ModelSpace.AddLine (StartPoint, EndPoint) 'Update the drawing display myLine.Update 'Display the dialog again Me.Show End Sub
The LINE1 program demonstrates the use of two Textboxes to gather input and two command buttons to control the dialog box and program execution.
Because of limited space, the program doesn't check the values entered. You should add this capability to your final program. If the values are incorrect, the focus should return to the TextBox with the invalid input. The first TextBox requests the distance for the line. A second TextBox lets you type the angle of the line. When you select OK, the prompt for the start point of the line appears. Once you select the start point, the program creates the line using the specified length and angle. The dialog box displays again for creation of another line until you select Cancel.
SAVING AND RUNNING THE PROGRAM
After you save the program, click the Run icon in the toolbar. The user interface appears in the AutoCAD window.
In the Distance text box, type in a value of 48. Press the
In her easy-to-follow, friendly style, long-time Cadalyst contributing editor and Autodesk Technical Evangelist Lynn Allen guides you through a new feature or time-saving trick in every episode of her popular AutoCAD video tips. Subscribe to the free Cadalyst Video Picks newsletter and we'll notify you every time a new video tip is published. All exclusively from Cadalyst!