Overview of Objects, Properties, Methods, and Events (VBA Programming Concepts) (AutoCAD VBA) Part 3

All about Methods

A method is a procedure that performs some action on the object with which it is associated. Some examples of methods that belong to the AutoCAD drawing object are AddText, AddLine, GetAngle, and Rotate. When called, some methods have parameters to which you assign values by listing arguments in the same order as the parameters are declared. The pop-up text from the Parameter Info editing feature gives you the parameters in the order required. This order allows the interpreter to match up the parameters with the arguments.

The Rotate method has two parameters: the BasePoint which is the center of rotation, and the RotationAngle which is the angle to be rotated. These parameters are listed in a pop-up when you type Rotate into the Code window, followed by a space, as shown here:

tmp757e-189_thumb[2]_thumb


The method’s parameters can be passed values using arguments, as follows:

tmp757e-190_thumb[2]_thumb

In this example, the variable CenterOfRotation argument is a three-element array to which you pass the x-, y-, and z-coordinates of a 3D point. The variable RotationAngle argument is passed an angle in radians as required by the Rotate method.

Other methods, such as GetAngle, return a value, so you need to assign the value returned to a variable, for example:

tmp757e-191_thumb[2]_thumb

In an assignment statement, the interpreter still requires the parenthesis after the method’s name even though there are no arguments to be passed.

When reading the properties and methods attached to an object, it is often less confusing to read from right to left. For example, the statement ReturnedAngle = ThisDrawing.Utility.GetAngle() can be read as "The variable ReturnedAngle is assigned the value returned by the GetAngle method of the Utility object, that is identified by the Utility property of the active document object named ThisDrawing. "

All about Events

UserForms and the controls placed on them from the Toolbox all have their own sets of properties and methods that work in much the same way as those from the AutoCAD object model. Because these objects are part of the GUI, they are expected to respond to a user’s actions, so they have their own predefined set of event procedures. For each control, it’s up to you to decide what events your application is going to respond to and what the reaction will be.

The Microsoft Windows operating system continually monitors all the open windows on your PC for any sign of activity (events) from the user, from a program, or from a window. When an activity occurs, a message is sent to the operating system, which passes it around to all the open windows and their controls. Visual Basic automatically processes any relevant messages. If you’ve written code for a particular event happening to a specific control and the message comes in that the event has occurred, Visual Basic executes the code from that control’s event procedure. This style of processing is called event driven.

Visual Basic’s event-driven model can catch you up sometimes; you must remember that running code from one event can sometimes trigger another event procedure. For example, if you have coded the Change event procedure for a TextBox control and your code assigns a new text string to that text box, your Change event procedure will be invoked, which may cause your program to perform in some unexpected way that could even lead to an abnormal termination.

The set of executable events varies from control to control, but there is a lot of overlap, and often the same event is common to many controls. For example, the Click event is found in UserForms, text boxes, command buttons, option buttons, check boxes, and lots more.

The event procedures have names made up of the control’s Name property, then an underscore character ( _ ), followed by the name of the event. Here are a few examples:

tmp757e-192_thumb[2][2]

The IDE provides pairs of first and last statements for all event procedures—I refer to these throughout the topic as skeleton procedures. All you need to do is decide which user events you want to respond to, and code your response in the appropriate skeleton.

Next post:

Previous post: