AfraLisp Blog

The AutoLisp/Visual Lisp/VBA Resource Website

AfraLisp Blog

Home Newsletter Utter Rubbish Coding Tips AutoCAD Tips Contact Downloads WAUN

Dialogue Box Default Data

Dialogue Defaults

When a user works with a dialogue box he enters the required data and then exits the dialogue box. On returning to the dialogue box, the data contained in the box should be updated to reflect the users previous entries. Most users tend to re-use the same set of data time after time, and it can be very frustrating to have to retype the same data every time you re-open a dialogue box.

To create defaults in a dialogue box is a 5 step process :

  • Retrieve the global variables containing the default data.
  • If they do not exist, create some default data.
  • Display the default data to the user.
  • Allow the user to change the data.
  • When the user exits the dialogue box, store this data as defaults.

The following application will show you how to do this :

DCL Coding:

test1 : dialog {				//dialog name
          label = "Dialogue Defaults" ;		//give it a label

	: row {					//define row

	: toggle {				//define toggle
	  key = "tog1";				//give it a name
	  label = "On or Off";			//give it a label
        }					//end toggle

	: toggle {				//define toggle
	  key = "tog2";				//give it a name
	  label = "Left or Right";		//give it a label
        }					//end toggle

	}					//end row

	spacer;					//put in a space

        : edit_box {				//define edit box
         key = "eb2" ;				//give it a name
         label = "&Enter a couple of Letters" ;	//give it a label
        }					//end edit box

	spacer;					//put in a space

        : edit_box {				//define edit box
         key = "eb1" ;				//give it a name
         label = "&Value of Slider" ;		//give it a label
         edit_width = 6 ;			//6 characters only
        }					//end edit box

        : slider {				//define slider
        key = "myslider" ;			//give it a name
        max_value = 100;			//upper value
        min_value = 0;				//lower value
        value = "50";				//initial value
	small_increment = 2;			//define small increment
	big_increment = 5;			//define big increment

        }					//end slider

	spacer;					//put in a space

        ok_cancel ;				//predefined OK/Cancel button

        }					//end dialog 

AutoLISP Coding:
(defun C:test1 ()
;define function	

;;;*****************************************************
;;;This section sets up the dialogue box default settings.

  (if (not lngth)
  ;if no default

	(setq lngth "50.0")
        ;set a default value

  );if
  ;slider and slider edit box value

  (if (not letters)
  ;if no default

	(setq letters "ABC")
	;set default value
  );if

  (if (not toggle1)
  ;if no default

	(setq toggle1 "1")
	;set default value

  );if

  (if (not toggle2)
  ;if no default

	(setq toggle2 "0")
	;set default value

  );if
;;;**********************************************************

  (setq dcl_id (load_dialog "test1.dcl"))
  ;load dialog

  (if (not (new_dialog "test1" dcl_id)
  ;test for dialog

      );not

    (exit)
    ;exit if no dialog

  );if
;;;******************************************************************
;;;This section sets the dialogue box default values

  (set_tile "tog1" toggle1)
  ;set toggle

  (set_tile "tog2" toggle2)
  ;set toggle

  (set_tile "eb2" letters)
  ;put data into edit box

  (set_tile "eb1" lngth)
  ;put data into edit box

  (set_tile "myslider" lngth)
  ;set the slider

;;;*********************************************************************
;;;This section retrieves the dialogue box values

  (action_tile "myslider"
  ;if user moves slider

	 "(slider_action $value $reason)")
         ;pass arguments to slider_action

  (action_tile "eb1"
  ;if user enters slot length

	 "(ebox_action $value $reason)")
         ;pass arguments to ebox_action

  (defun slider_action (val why)
  ;define function

      (if (or (= why 2) (= why 1))
      ;check values

      (set_tile "eb1" val)))
      ;update edit box

  (defun ebox_action (val why)
  ;define function

      (if (or (= why 2) (= why 1))
      ;check values

      (set_tile "myslider" val)))
      ;update slider

   (action_tile "tog1"
   ;if toggle 1 selected

      "(setq toggle1 $value)"
       ;get the value

   );action_tile

   (action_tile "tog2"
   ;if toggle 2 selected

      "(setq toggle2 $value)"
       ;get the value

   );action_tile

   (action_tile
      "accept"	
      ;if O.K. pressed

      "(progn 
       ;do the following

      (setq lngth (get_tile \"eb1\"))
      ;get the edit box value

      (setq letters (get_tile \"eb2\"))
      ;get the slider value

       (done_dialog) (setq userclick T))"
       ;close dialog, set flag	
	
   );action tile

    (action_tile
    "cancel"
    ;if cancel button pressed

    "(done_dialog) (setq userclick nil)"	
    ;close dialog and clear flag

    );action_tile
;;********************************************************************

  (start_dialog)	
  ;start dialog
				
  (unload_dialog dcl_id)
  ;unload	

   (if userclick
   ;check O.K. was selected

      (alert (strcat "You Selected: " lngth " and " letters))			
      ;display the Data

  );if userclick

 (princ)

);defun

(princ)

Now load and run this application in AutoCAD. Enter some new data and exit the dialogue box. Run the application again. You will find that the dialogue box
has 'remembered' your entries.

Now this is fine, but if you start a new drawing, or exit AutoCAD these values will be lost. Let's re-write this routine to "permanently" remember these values.
To do this we will make use of Application Data. Here's how :

DCL Coding:

test2 : dialog {				//dialog name
          label = "Dialogue Defaults" ;		//give it a label

	: row {					//define row

	: toggle {				//define toggle
	  key = "tog1";				//give it a name
	  label = "On or Off";			//give it a label
        }					//end toggle

	: toggle {				//define toggle
	  key = "tog2";				//give it a name
	  label = "Left or Right";		//give it a label
        }					//end toggle

	}					//end row

	spacer;					//put in a space

        : edit_box {				//define edit box
         key = "eb2" ;				//give it a name
         label = "&Enter a couple of Letters" ;	//give it a label
        }					//end edit box

	spacer;					//put in a space

        : edit_box {				//define edit box
         key = "eb1" ;				//give it a name
         label = "&Value of Slider" ;		//give it a label
         edit_width = 6 ;			//6 characters only
        }					//end edit box

        : slider {				//define slider
        key = "myslider" ;			//give it a name
        max_value = 100;			//upper value
        min_value = 0;				//lower value
        value = "50";				//initial value
	small_increment = 2;			//define small increment
	big_increment = 5;			//define big increment

        }					//end slider

	spacer;					//put in a space

        ok_cancel ;				//predefined OK/Cancel button

        }					//end dialog 

AutoLISP Coding:
(defun C:test2 ()
;define function	

;;;*****************************************************
;;;This section retrieves the default data from the
;;;ACADR14.CFG file.


    (setq lngth (getcfg "AppData/CadKen/default1"))
    ;get the apllication data

       (if (= lngth nil)
      ;if it is nil

         (progn
         ;do the following
                 
	  (setcfg "AppData/CadKen/default1" "50.0")
	  ;set the default data 

          (setq lngth (getcfg "AppData/CadKen/default1"))
          ;retrieve the default data

         );progn 

      );if

    (setq letters (getcfg "AppData/CadKen/default2"))
    ;get the aplication data

       (if (= letters nil)
      ;if it is nil

         (progn
         ;do the following
                 
	  (setcfg "AppData/CadKen/default2" "ABC")
	  ;set the default data 

          (setq letters (getcfg "AppData/CadKen/default2"))
          ;retrieve the default data

         );progn 

      );if

  
    (setq toggle1 (getcfg "AppData/CadKen/default3"))
    ;get the apllication data

       (if (= toggle1 nil)
      ;if it is nil

         (progn
         ;do the following
                 
	  (setcfg "AppData/CadKen/default3" "1")
	  ;set the default data 

          (setq toggle1 (getcfg "AppData/CadKen/default3"))
          ;retrieve the default data

         );progn 

      );if

    (setq toggle2 (getcfg "AppData/CadKen/default4"))
    ;get the apllication data

       (if (= toggle2 nil)
      ;if it is nil

         (progn
         ;do the following
                 
	  (setcfg "AppData/CadKen/default4" "0")
	  ;set the default data 

          (setq toggle2 (getcfg "AppData/CadKen/default4"))
          ;retrieve the default data

         );progn 

      );if

;;;**********************************************************

  (setq dcl_id (load_dialog "test2.dcl"))
  ;load dialog

  (if (not (new_dialog "test2" dcl_id)
  ;test for dialog

      );not

    (exit)
    ;exit if no dialog

  );if
;;;******************************************************************
;;;This section sets the dialogue box default values

  (set_tile "tog1" toggle1)
  ;set toggle

  (set_tile "tog2" toggle2)
  ;set toggle

  (set_tile "eb2" letters)
  ;put data into edit box

  (set_tile "eb1" lngth)
  ;put data into edit box

  (set_tile "myslider" lngth)
  ;set the slider

;;;*********************************************************************
;;;This section retrieves the dialogue box values

  (action_tile "myslider"
  ;if user moves slider

	 "(slider_action $value $reason)")
         ;pass arguments to slider_action

  (action_tile "eb1"
  ;if user enters slot length

	 "(ebox_action $value $reason)")
         ;pass arguments to ebox_action

  (defun slider_action (val why)
  ;define function

      (if (or (= why 2) (= why 1))
      ;check values

      (set_tile "eb1" val)))
      ;update edit box

  (defun ebox_action (val why)
  ;define function

      (if (or (= why 2) (= why 1))
      ;check values

      (set_tile "myslider" val)))
      ;update slider

   (action_tile "tog1"
   ;if toggle 1 selected

      "(setq toggle1 $value)"
       ;get the value

   );action_tile

   (action_tile "tog2"
   ;if toggle 2 selected

      "(setq toggle2 $value)"
       ;get the value

   );action_tile

   (action_tile
      "accept"	
      ;if O.K. pressed

      "(progn 
       ;do the following

      (setq lngth (get_tile \"eb1\"))
      ;get the slider value

      (setq letters (get_tile \"eb2\"))
      ;get the edit box value

       (done_dialog) (setq userclick T))"
       ;close dialog, set flag	
	
   );action tile

    (action_tile
    "cancel"
    ;if cancel button pressed

    "(done_dialog) (setq userclick nil)"	
    ;close dialog and clear flag

    );action_tile
;;********************************************************************

  (start_dialog)	
  ;start dialog
				
  (unload_dialog dcl_id)
  ;unload	

   (if userclick
   ;check O.K. was selected
;;;*******************************************************************
;;;This section stores the data into the AACDR14.CFG file and displays
;;;them to the user.

    (progn

      (setcfg "AppData/CadKen/default1" lngth)
      ;store the data

      (setcfg "AppData/CadKen/default2" letters)
      ;store the data

      (setcfg "AppData/CadKen/default3" toggle1)
      ;store the data

      (setcfg "AppData/CadKen/default4" toggle2)
      ;store the data

      (alert (strcat "You Entered: " lngth " and " letters))			
      ;display the Data

    );progn

  );if userclick

 (princ)

);defun

(princ) 

Now load and run this application. Again, add some new values and exit the dialogue.
Now exit AutoCAD. Re-enter AutoCAD and load and run the application again.
Your values have been retained.... 
To download the source coding for this tutorial click here
 
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