AutoCAD VBA Programming: Tools and Techniques

Event Programming Example

The following is a simple example of event programming. The application is to keep track of the time spent working in a window. When the window is no longer active, the system will report how much time was spent there.

  Dim Time_Start  Dim Dwg_Name As String  '  Private Sub AcadDocument_Activate()    Time_Start = Timer    Dwg_Name = ThisDrawing.FullName  End Sub  '  Private Sub AcadDocument_Deactivate()    MsgBox "Time Spent in" & Dwg_Name & "was" & Timer -  Time_Start  End Sub

There are two functions involved here: the Activate and Deactivate events. When the window is activated, the Activate event will be called. At this time, we will save the current value of Timer, VBA function that returns the number of seconds since midnight. We will also save the drawing name for future reference in the program. The drawing name is saved in the variable Dwg_Name. The full name of the drawing is obtained from full name property of the current drawing object.

The second function in this example is the Deactivate event. This function is called when the current drawing window is no longer current. This event will occur when the user selects a different drawing in the AutoCAD MDI environment. (Note that when you are working in the VBAIDE, leaving AutoCAD to return to the editor seems to trigger this event too.)

As just seen, event-based programming can offer some powerful...

UNLIMITED FREE
ACCESS
TO THE WORLD'S BEST IDEAS

SUBMIT
Already a GlobalSpec user? Log in.

This is embarrasing...

An error occurred while processing the form. Please try again in a few minutes.

Customize Your GlobalSpec Experience

Category: IC Timers
Finish!
Privacy Policy

This is embarrasing...

An error occurred while processing the form. Please try again in a few minutes.