Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Is there a way to save and restore a forms objects properties?

Status
Not open for further replies.

jbailey268

Programmer
May 25, 2005
51
US
I would like to know if there is a way to save in a file (or perhaps a memo field) all of the object's properties, .enabled, .visible, .caption .value .displayvalue etc.
Then a user can "Save Preferences" exit the application and when the user goes back in next time; can "Restore Last Preferences" and voila there they are.

Currently I have 100+ objects, and I save various properties to an array, and save the array in a mem file, and save the mem file in a memo field in a table.

ex) in saving...
aObjecs(191)= thisform.bulkframe1.page2.licensetype.value

upon restore the reverse
Thisform.Bulkframe1.page2.licensetype.value=aObjects(191)


I have to port this to another application and it is so arduous to have to handcode and debug roughly 200 lines in the similar fashion .

Is there a better way?
 
some functions, which help you: Amembers(), insert from name oObject, scatter to name. (Needs VFP9)

To say a little more, if you define a cursor with the object properties you want to save, then you can eg. insert from name Thisform.Text2. And do so for several objects. And Scatter Name also allows to scatter to Thisform or Thisform.Text2. You even can do a bulk-Addproperty(), if you define fields in the cursor, the object does not have natively, if you use the Additive clause of Scatter, new in VFP9.

Bye, Olaf.
 
Forgot to mention I'm using VFP8

aMembers sounds like it has some play. I'll try that.

Thanks
 
Amembers "Places the names of properties, procedures, and member objects for an object into a variable array." this does not give you the values, enabled etc that you want to restore.
What you want to do involves programming. One way which comes to mind is to store in a table. You will have to loop through the form and its objects using:Controls[ ] (an array built in VFP) with Controlcount.

for x = 1 to this.controlcount
replace cname with this.controls[x].name
......
Every object then becomes an array (this.controls[1] etc) select the one you want and save to a table.
If a restore is required, Scan the table in the forms Init and reset the properties again using controls array.
Look up controls and controlcount in help.
You can identify other containers in the form ie Pageframe by
If this.controls[x].baseclass = "Pageframe"
for i = 1 to this.pageframe.controlcount
this.pageframe.controls ...

etc.
Its tedious to code but not at all difficult. Once done it can be run on any object.

 
Or you can save to your array
for x = 1 to this.controlcount
YourArray[x] = this.controls[x].name
etc
 
Hi Jbailey268,

I suggest you don't try to save all the properties in this way. If your aim is to let the user save and restore their preferences, you should only save those properties that are directly settable by the user.

Many of an object's properties need to properly initialised when the object is instantiated, and they shouldn't just be set to whatever value they contained at the last shutdown.

I suggest you use a table to save the properties that are relevant to the user's preferences or settings. The table would need a field for the property name, one for the value, and possibly one for the data type. You also need to store the user name or ID, so that each user can maintain their own set of preferences.

I wrote an article in Foxpro Advisor, September 06 ( that shows how to do this. But don't worry if you don't have the article -- it's pretty easy to cook it up yourself.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

My Visual FoxPro site: www.ml-consult.co.uk
 
Good advices indeed.

Amembers still can help you find out properties you want to save in a generic way. Unfortunately some features I suggested are only available with VFP9, but amembers also allows you to detect userdefined properties or proerteis, that were changed (cFlags-Parameter). Which means, changed from their default value.

If you eg do some form resizing, you could store the changed position and size. On the other side you'll have some code, which resizes the form, so it would be sufficient to store orm size only and then run the resizing code initially and do programmatically, what would otherwise be triggered interctively.

Don't simply store anything. Mike is right, especially controlsource or bindcontrols-Property should not be reset at any time to the last value they had, that could cause strange effects, especially in grids.

Bye, Olaf.
 
Thanks for your help.

It's true that I don't save everything, label captions are not candidate for saving - they are static for everyone.

FYI:
Also this question was put on the VFP General Issues page.
Tamer Granor suggested a nice solution. Then the next day I received an e-mail telling me I would get a better response if I posted it on Forms Class and Controls. But it was "GONE" - disappeared completely.And I had to re-type the e-mail again on this forum.

But thank you ALL for your generous help.
I thought there would still be some "programming" involved. I just basically needed to feel validated that there wasn't some simple object save, object restore command somewhere that I missed.
 
There is one thing quite easy and short in code, that is to use the SaveAsClass() Method of the form class (or SaveAs with SCX forms) and use the last saved class/scx version for the next instance of the form.

But I wouldn't go that route.

For example because you would create and maintain external files. And I'd expect side effects from it, eg form controls might be bound to cursors, which are created at runtime and not available at it's init(). That would make these saved forms quite unusable.

Bye, Olaf.
 
Sorry the other answer got lost. The basic idea is to save the name of the control, the name of the property, the type and the value as a record in a table. Read the table on the way in, save it on the way out. You use recursion to drill down through the controls on the form.

Tamar
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top