AfraLISP - Learn AutoLISP for AutoCAD productivity

VBA Primer - Part 3

by Kenny Ramage

OK, we now have our basic dialogue box and would like it to do something. Firstly, we need to code the cancel button so that we can exit the application while we are still testing. We also need to create a Module so that we can run the application from within AutoCAD.

Let's create the Module first. Select InsertModule. A new window will open looking like this:

New Module

This is the code window were you will be writing all of your Visual Basic Code. If you look at the Project window you will find that a new Module has been created and has been given the default name of Module1.

Now enter the following code into the module :

	Sub chlayer ()
	'name of function
 
		UserForm1.Show
		'display the dialogue box
		'UserForm1
 
	End Sub

Thw code window should look like this :

Code Window

As you have probably worked out for yourself, any line starting with a ' is a comment. (The same as the ; in AutoLISP). Visual Basic also colour codes the coding but I am not going to get into that at this time.

Next, double click on the Cancel button on UserForm1. The code window will open again, displaying the Click Event procedure for that control. Type in the following code :

	Private Sub CommandButton2_Click()
 
    		End
    		'end the programme
    
	End Sub

The code window should look like this :

Cancel code

This sub-routine simply ends the programme if the Cancel button is selected.

Now, let's test our dialogue box. (This would be a good time to save your project…)

Return to AutoCAD and select ToolsMacroMacros from the menu bar. The following dialogue should appear:

Macro Dialogue

Select the module if it isn't already selected. Now click Run. Your dialogue should appear in all it's glory. (Thunderous applause from the audience.) Once you have finished admiring your handywork select Cancel.

We still have a couple of problems though. Firstly, there is nothing in the list box and the OK button doesn't work. In Part 4 we'll show you how to populate the list box with all the layer names in your drawing and then we'll write the main event procedure for the O.K. button.

While you are waiting for the next page you might as well grab yourself a beer. I've got one…