Statalist The Stata Listserver


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: Dialog programming: (I)what lists can be passed (II)How to put quotes


From   "James Hassell" <jhassell@stata.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: Dialog programming: (I)what lists can be passed (II)How to put quotes
Date   Mon, 06 Nov 2006 10:08:29 -0600

-- David Elliott wrote --

>(I)
>I basically want a dialog to do the following:
>
>1) if no dataset is open, open a dataset or, if one is open, use it or
>open a different one.  I know how to do this with the FILE control and
>filter() option
>
>2) subsequently, I want to populate two variable lists to be used in
>COMBOBOX dropdownlists.  They should consist of (a) a list filtered to
>include only numeric variables and (b) a list of all variables (I
>don't want to use the VARLIST control unless I absolutely have to)
>
>I have already figured out that, since Stata must load the file first
>to populate the lists, I have to have an intermediate step to open the
>file.  Also, since the dialog must wait for that action, I have made
>it a SYNCHRONOUS_ONLY dialog.
>
>I'd like to populate my lists with:
>
>PROGRAM LOAD_VAR /*Load Measure and Grouper varlists*/
>BEGIN
>foreach var of varlist * {
>	local type : type `var'
>	global allvar $allvar `var'
>	if inlist("`type'","byte","int","long","float","double") {
>		global measlist $measlist `var'
>		}
>	}
>END
>
>or, alternatively, something like
>
>PROGRAM LOAD_VAR /*Load Measure and Grouper varlists*/
>BEGIN
>put "quietly ds, not(type string)"
>stata
>global measlist `r(varlist)' /* would contain all numeric variables */
>put "quietly ds"
>stata
>global allvar `r(varlist)'
>END
>
>I can see that my lists have been created by issuing a post dialog
>-mac dir-. Unfortunately, I can't tell if this sort of construct is
>allowed for COMBOBOX controls although you appear to be able to pass
>global strings to an EDIT control.  So am I trying to micturate to
>windward here?  I can see other situations where one might want to
>pass lists to a combobox, for example have contents(variable labels)
>values(variable list). (I am programming dialogs for non/occasional
>users of Stata and am trying to insulate people form cryptic variable
>names)  I have had no difficulty programming comboboxes with this type
>of construct in other languages so I am surprised I can't seem to do
>it here.
>
(II)
Another issue I have been unable to resolve is how to *put* a "quoted
string" onto the rstring.  As a silly example:
>
>PROGRAM command
>BEGIN
>	put "di "
>	put "Goodbye cruel world"
>END
>
>The quotes are stripped so the output command is -di Goodbye cruel
>world- rather than -di "Goodbye cruel world"- and attempts at various
>quote combinations and attempts to use separate puts with char(34) are
>to no avail, nor is any use of escaped characters.

-- 

Dialog boxes do not have some of the constructs needed to program what
you want, so you will need to write an ado program which can be called 
from the dialog box as it loads. Below you will find an example that 
does what you have asked. Note that in the ado program I reference
.d1_dlg.variables.

.d1_dlg --- The name of the dialog box specified by the filename with "_dlg"
appended to the end.
 
 .variables is the name of the LIST which was specified by the dialog box, 
 and also linked to the COMBOBOX by its contents option.
 
 
 See -help dialog_programming- Appendix B. For more information on
 class programming with dialogs.
 	

/************ Begin d1.dlg ************/
/*
  
*!  VERSION 1.0.0  06nov2006

*/

VERSION 9
SYNCHRONOUS_ONLY
POSITION . . 400 300
COPY 	copy1
OK ok1,	label("OK")
CANCEL	can1, label("Cancel")
SUBMIT	sub1, label("Submit")
RESET	res1

PROGRAM PREINIT_PROGRAM
BEGIN
	put "d1_init"
	stata hidden
END

DIALOG main, label("Test COMBOBOX control as varlist")
BEGIN
  TEXT     tx_cb1	10	10	100	.,	///
	label("Variable:") right		
  COMBOBOX cb1		+105	@	240	.,	///
  	dropdownlist contents(variables)
END

LIST variables
BEGIN
	// intentionally empty
	// this list will be populated by d1_init.ado
END

PROGRAM command
BEGIN
    /* quoting examples */
	put "di "

	put  `"""'		// open quote
	put "Hello cruel world... "
	put `"""' 		// closed quote

	put " "

	put "`" `"""'	// open compound quote
	put "Goodbye cruel world"
	put `"""' "'"	// closed compound quote
END
/************ End d1.dlg ************/


/************ Begin d1_init.ado ************/
program d1_init
	version 9
	syntax [varlist]

	unab varlist : `varlist'
	if "`varlist'" != "" {
		.d1_dlg.variables.Arrdropall
	}
	foreach v of local varlist {
		capture confirm numeric variable  `v'
		if ! _rc {
			.d1_dlg.variables.Arrpush "`v'"
		}
	}
end
/************ End d1_init.ado ************/
*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index