Community Tip - Need to share some code when posting a question or reply? Make sure to use the "Insert code sample" menu option. Learn more! X
The question of how to save the state of controls in a Mathcad worksheet comes up so often I decided to post the answer somewhere I can easily find it and post a link to it!
There are two approaches:
1) Save the state of the control in the worksheet custom metadata. This method will work in versions 13, 14 and 15, but not in versions 11 or earlier (I"m not sure about version 12).
2) Save the state of the control in a textbox. This will work in all versions 11 and later, and maybe some earlier versions too.
There are some things to be aware of when using them.
Firstly, in version 11 if the result of calling a function is not displayed, and it's also not assigned to any variable, Mathcad does not calculate it. That means that if you use method 2 and put the SaveData function in a collapsed area it will not work. In version 12 and later they removed this optimization, and therefore it will work.
For the second method, if the Listbox is off screen at start-up and there is a calculation using the output from the Listbox in a collapsed area then Mathcad appears to generate a new (blank) Mathcad process on the windows taskbar. This problem applies only to version 12
Saving the state in the custom metadata causes another Mathcad.exe process to be started. There is at least one report that in version 14 Enterprise Edition (this does not apply to the Single User Edition) this new Mathcad.exe process requires an additional license, so you get a popup error every time you run the program. I don't know if this has been fixed for later service releases of version 14, but it does not occur in version 15.
Saving the state of the control in the custom metadata will not work if the control is in a referenced worksheet (because it can't see the metadata). Method 2 will work if the control, and SaveData function, are in a referenced worksheet.
Method 1 is much slower (about 6 times slower!) than method 2. This doesn't matter much if you have only one control, but it can make a big difference if you have a large number of them.
04 Feb 12: Added listboxes that are dynamically filled from an input.
07 Nov 2014: Added a checkbox that saves it's state in custom metadata.
14 Nov 2014: Improved the dynamically filled listboxes to take a scalar input for a single element list.
Just wondering on how to implement what you're presenting here. On the surface level it seems pretty easy, but when I try to recreate it in my own worksheets it doesn't work. I looked at the coding for your function and controls, and it's apparent that things are not as they first seemed. I don't know much about programming, so it makes it seem as though a lot went into this. So when getting this to work in my own sheet, is this meant to be an example and should be personally coded each time, or can it just be copied directly?
You do not need to change the script inside the controls. Just copy the controls into your worksheet. The version that saves the state in the worksheet metadata is less confusing, so as long as your version of Mathcad supports metadata you should probably use those controls.
If you can't get it to work, post the worksheet to the forums. I will be out of the country from tomorrow until July 6th, but someone else can probably show you what the problem is.
That's great. Thanks!
Do you have an example with a checkbox?
I do now
Thanks a lot!
Thanks for the single input option. Had been struggling for a while trying to get that working.