AfraLisp Blog

The AutoLisp/Visual Lisp/VBA Resource Website

AfraLisp Blog

Home Newsletter Utter Rubbish Coding Tips AutoCAD Tips Contact Downloads WAUN

Home.

Page I.

Getting Started - Page II

OK, tea break over, back to work!!

Copy and paste this into Notepad and save it as "TEST_DCL2.DCL".

//DCL CODING STARTS HERE
test_dcl2

: dialog

{

label = "Test Dialog No 2";
		
	: edit_box
	{
	label = "Enter Your Name :";
	mnemonic = "N";
	key = "name";
	alignment = centered;
	edit_limit = 30;
	edit_width = 30;
	}

	: edit_box
	{
	label = "Enter Your Age :";
	mnemonic = "A";
	key = "age";
	alignment = centered;
	edit_limit = 3;
	edit_width = 3;
	value = "";
	}

	: button	
	{
	key = "accept";
	label = "OK";
	is_default = true;
	fixed_width = true;
	alignment = centered;
	}

	: errtile
	{
	width = 34;
	}
}
//DCL CODING ENDS HERE

And now the AutoLisp coding. Copy and paste this and save it as "TEST_DCL2.LSP" and then load and run it :
;AUTOLISP CODING STARTS HERE
(prompt "\nType TEST_DCL2 to run.....")

(defun C:TEST_DCL2 ( / dcl_id)

(setq dcl_id (load_dialog "test_dcl2.dcl"))
     (if (not (new_dialog "test_dcl2" dcl_id))
	 (exit )
     );if

(set_tile "name" "Enter Name Here")
(mode_tile "name" 2)
(action_tile "name" "(setq name $value)")
(action_tile "age" "(setq age $value)")
(action_tile "accept" "(val1)")

(start_dialog)
(unload_dialog dcl_id)

(alert (strcat "Your name is " name
               "\nand you are " age " years of age."))

(princ)

);defun

-----------------------

(defun val1 ()

(if (= (get_tile "name") "Enter Name Here")
	(progn
	   (set_tile "error" "You must enter a name!")
	   (mode_tile "name" 2)
	);progn
	(val2)
);if

);defun

-------------------

(defun val2 ()

(if (< (atoi (get_tile "age")) 1)
	(progn
	  (set_tile "error" "Invalid Age - Please Try Again!!")
	  (mode_tile "age" 2)
	);progn
	(done_dialog)
);if

);defun

(princ)
;AUTOLISP CODING ENDS HERE



The dialog that is displayed contains two edit boxes. One to enter your name into, and one your age. After entering the relevant information select the "OK" button. An alert box will display showing your details.

Now run the program again, but do not enter your name. An error message will display informing you that you must enter your name.


Fill in your name but leave your age blank. Press enter again. Another error message will inform you that you have entered an invalid age.

Try it with a zero or a negative number. The same error message will display until you enter a number equal or greater than 1. Both of these examples are know as validation.

Let's have a look at the DCL coding.

:edit_box - This is a predefined tile that allows the user to enter or edit a single line of text.
There are a few new attributes defined in this tile :

mnemonic = "A"; - This defines the mnemonic character for the tile.
edit_limit = 3; - This limits the size of the edit box to 3 characters.
edit_width = 3; - This limits the user to typing a maximum of 3 characters.
value = ""; - This sets the intial value of the edit box, in this case nothing.

Now let's have a look at the AutoLisp coding. First the "action expressions".

(set_tile "name" "Enter Name Here") - This sets the runtime (initial) value of a tile who's "key" attribute is "name".

(mode_tile "name" 2) - This sets the mode of the tile who's "key" attribute is "name". In this example, we have used a mode of "2" that allows for overwriting of what is already in the edit box.

(action_tile "name" "(setq name $value)") - Associates the specied tile with the action expression or call back function. In this case we are saying "If the tile who's "key" attribute is "name" is selected, store the value of this tile in the variable name."

(get_tile "name") - Retrieve the value of the tile who's "key" attribute is "name".

Did you notice that we wrote :
(action_tile "accept" "(val1)")
instead of :
(action_tile "accept" "(done_dialog)")

Instead of closing the dialog when OK is selected, we initiate a subroutine that checks (validates) that the name information is exceptable. If it is not, the error tile is displayed.
(set_tile "error" "You must enter a name!")

Once the name has been validated, this routine then initiates a second subroutine that validates the age value.
If this not correct, the error tile is again displayed, this time with a different message.
(set_tile "error" "Invalid Age - Please Try Again!!")
If everything is correct, (done_dialog) is called, all values are returned and the alert box is displayed with the relevant information.

 
The AutoLisp/Visual Lisp/VBA Resource Website

Copyright © 1999-Perpetuity by AfraLisp

All rights reserved.
Information in this document is subject to change without notice.
Site created and maintained by Kenny Ramage

The AutoLisp/Visual Lisp/VBA Resource Website