AutoCAD

master VBA dialog boxes

1 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
Figure 1. TextBox control lets you enter a single line of text as input to a VBA routine.
This month we'll create a dialog box for input to create a line. We'll use the TextBox control to obtain the line's distance and angle. A TextBox control lets you edit or enter a single line of text. Figure 1 shows the size and placement of the controls.

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 Code
Download 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).
Add a Label control for both the Distance and Angle Textbox controls.

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
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.


Private Sub Cancel_Click()
    End
End Sub
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 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
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 key to move to the Angle text box and type 45. This represents a line 48" long at a 45


AutoCAD Tips!

Lynn Allen

Autodesk Technical Evangelist Lynn Allen guides you through a different AutoCAD feature in every edition of her popular "Circles and Lines" tutorial series. For even more AutoCAD how-to, check out Lynn's quick tips in the Cadalyst Video Gallery. Subscribe to Cadalyst's Tips & Tricks Tuesdays free e-newsletter and we'll notify you every time a new video tip is available. All exclusively from Cadalyst!
Follow Lynn on Twitter Follow Lynn on Twitter


Latest News from Cadalyst Partners
Feed
Life of Pi shows why Robots will one day have emotions   20 May, 2013

"I love my computer  you make me feel alright  every waking hour  and every lonely night" — Bad...More>>Read more It's Alive in the Lab blog posts>>

Feed
Webcast Replay: Flexible Design with PTC Creo   20 May, 2013

If you’re already familiar with Pro/ENGINEER, you’ll find PTC Creo familiar and intuitive in many ways, especially with the newly added PTC Creo...More>>Read more PTC Creo blog posts>>

Feed
Autodesk Revit Server 2014 - Now available for subscription customer download   20 May, 2013

Collaborate...coordinated... seamlessly... integrated...team...Wow...sounds awesome. Know anyone using...More>>Read more BIMbuilder blog posts>>

Feed
Discover and Fix Your Vibration Vulnerability with SolidWorks Simulation   17 May, 2013

When I go biking during the summer, I rely on the frame of my bicycle to withstand stresses such as vibration and impact. As I speed down the trail,...More>>Read more SolidWorks Blog posts>>

Feed
British Engineering in Effect. Flat-pack Truck Design for Developing Countries.   20 May, 2013

Imagine a crate of trucks that can be shipped on-site, built within a day, then sent to the four corners of the country delivering food, people,...More>>Read more SolidSmack blog posts>>

Poll
What type of input device (besides the keyboard) do you use at your primary CAD workstation?
Standard mouse
Trackball mouse
Programmable mouse
3D navigation device
Pen/tablet
Multiple devices
Submit Vote




Considering Wide Format Printing Solutions?
Four FREE White Papers Available:






Subscribe Cadalyst Newsletters