"Tim Hofer" <[email protected]> asked:
> I may be asking too much of the dialog box commands but:
>
> I would like to present the user with 6 boxes/spinners and ask them
> to put in the proportion of cases falling into each of 6 groups.
>
> obviously the contents have to add to 100
>
> 1/ Is there some way to do mathematical operations on the input
> controls and check that they add to 100 as part of some iaction?
>
> or better yet have the 6th box (not set by the user) change to
> reflect changes in the other 5? (It seemed like this might be
> possible via the spinner onchange(iaction) but I got stuck).
What we (here at Stata) always did in this case was let the user
submit erroneous data and let the ado code complain. I think this is
truly your best solution here. I can think of quite a few pitfalls in
this scheme, one of which is for the user to put "99" in for each of
the first 5 spinners. If you think about modifying more spinners than
the last one you will quickly get a headache. I do think your best
bet is to let the user do stupid things and let the ado code sort it
out.
You can't modify that final spinner through the dialog programming
language, but, assuming I haven't talked you out trying this yet,
you can modify that final spinner's value via a trip through an
associated ado program and modify it via the class system. I can
tell you how to change the value of this final spinner, but you can't
change the default min and max values for it. Consequently the user
can still go in and change the spinner value.
Here's some sample code. I'm using 3 spinners instead of 6 for
simplicity, and I want their values to add to 100.
-------spin.dlg---------
/*
spin - fiddle around with spinners
*! VERSION 1.0.0 02may2003
*/
VERSION 8.0
POSITION . . 400 300
OK ok1, label("OK")
CANCEL can1, label("Cancel")
SUBMIT sub1, label("Submit")
HELP hlp1, view("help dialog")
RESET res1
DIALOG main, label("spin - fiddle with spinners") /*
*/ tabtitle("Main")
BEGIN
/*--+----1----+----2----+----3----+----4----+----5----+----6----+----7----+*/
TEXT tx_file 10 10 380 ., /*
*/label("Choose proportions:")
SPINNER sp_one 20 +20 60 ., /*
*/label("Spinner one") /*
*/min(0) max(100) default(1) /*
*/onchange("program spinner_chg")
SPINNER sp_two @ +25 60 ., /*
*/label("Spinner two") /*
*/min(0) max(100) default(1) /*
*/onchange("program spinner_chg")
SPINNER sp_three @ +25 60 ., /*
*/label("Spinner three") /*
*/min(0) max(100) default(98)
END
/* This PROGRAM calls the ado program spinchg with arguments that are
the values of the first two spinners */
PROGRAM spinner_chg
BEGIN
put "spinchg "
put main.sp_one " "
put main.sp_two " "
stata
END
/* A public service announcement since this is just a test */
/* the spinner group is then output */
/* and that's it. */
PROGRAM command
BEGIN
put "May is National Bike Month!"
put "group(" main.sp_one " " main.sp_two " " main.sp_three ") "
put "That's all folks! "
END
----------end spin.dlg--------------
Here's the associated ado file:
---------spinchg.ado---------------
*! VERSION 1.0.0 02may2003
program define spinchg
version 8
args spin1 spin2
confirm integer number `spin1'
confirm integer number `spin2'
local val = 100 - `spin1' - `spin2'
if (`spin1'+`spin2')>100 {
di in red "The first two spinners must add to a number"
di in red "less than or equal to 100."
local val 0
}
/* this next uses the class system to set the label */
.spin_dlg.main.sp_three.setvalue "`val'"
/* the spin_dlg names and defines the object
-- spin which is a dialog.
main is the name of the tab in dialog spin
sp_three is the control in the main tab
setvalue is what I want to do to sp_three
*/
end
--------end spinchng.ado----------------
Tim also asked:
> 2/ the results have to be presented to a program as an option. the
> optionarg command says that it is an easy way to add single argument
> options. What about multiple argument options? e.g. I want:
> groups(10 30 30 10 10 10) if I give each SPINNER control the option
> name "groups" then I get separate options group(10) group(30) etc
> using the optionarg command I can build it up w. 6 put commands but
> that seems cumbersome
This is also accomplished with the spin.dlg file above -- although I
suspect from your subsequent email that you've already figured this
out.
Enjoy!
--Jean Marie
[email protected]
*
* 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/