Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Using a prefernce/options form

Posted on 2004-08-30
5
Medium Priority
?
135 Views
Last Modified: 2010-04-05
Can someone explain how and where a person is suppose to put the code to save & load settings for an application if you use a settings form?
A standard settings form would have an "OK" | "Apply" | "Cancel" buttons on it.

Now obviously when a user selects "OK" the settings are saved and the settings form is closed. No Prob.
And when a User "Cancels", no settings are saved and the old settings should be reloaded and then the settings form is closed. No Prob.
"Apply" is the same as "OK" except no settings are saved.

Now the problem I have, is when and where do I load the settings when my application is started!
When I load the settings at startup, I cant set any of the components in my settings form unless my settings form is visible.
I thought all forms are created right away when an app is created?
Shouldnt you be able to change things on a form even if its hidden?

Am I doing this wrong? Should I load the settings into a global variable and then set the component on my settings form when it is shown?
And not use the components to directly access the settings? I mean, if I want to know if my app is suppose to startup to the tray, should I see if a certain checkbox is checked in the settings form, or should I use a global var? I want to use ReadComponent/WriteComponent to load & save my settings, or should I just use a simple ini?
0
Comment
Question by:LMuadDIb
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 12

Expert Comment

by:esoftbg
ID: 11939385
> "Apply" is the same as "OK" except no settings are saved.
- I think:
  "Apply" is the same as "OK" except the settings form is NOT closed.

> Shouldnt you be able to change things on a form even if its hidden?
- Yes, you can do that - if the name of your settings form is FormSettings,
  then you should use the event:
procedure TFormSettings.FormCreate(Sender: TObject);
begin
  CheckBox1.Checked := ....;
end;
0
 
LVL 17

Expert Comment

by:geobul
ID: 11939493
Hi,

>I thought all forms are created right away when an app is created?

This is the default way unless you remove a certain form from 'Auto-create forms' list (Project Options).

>Shouldnt you be able to change things on a form even if its hidden?

Yes, you should. Use forms OnCreate event for doing that for the first time (or initialization section of the forms unit or even override Loaded method of the form). You may also change things at any other time and event - the form of interest must be created beforehand, of course.

>I want to use ReadComponent/WriteComponent to load & save my settings, or should I just use a simple ini?

You may use ini file, windows registry or another file format (including database table, for instance).

>Now the problem I have, is when and where do I load the settings when my application is started!

These settings apply to all forms and their components in your app I suppose. You may do this in different ways:
- load all the settings at once from a file or registry in global variables or in your own component (OnCreate event of your main form, for instance) and then set them from these global vars using other forms OnCreate events or your own method.
- read the settings of a form from a file or registry in its OnCreate event and set them there.

I personally prefer the first way. My apps have a function ReadIniFile which is being called in OnCreate event of the main form and all settings are stored in global variables. In 'Settings form' I execute a global function 'ApplyAllSettings' which calls FormX.ApplySettings for all forms in the project.  FormX.ApplySettings is also being called in FormX.OnCreate.

>if I want to know if my app is suppose to startup to the tray, should I see if a certain checkbox is checked in the settings form, or should I use a global var?

Read and set application specific settings in OnCreate event of the main form, IMHO.

Regards, Geo
0
 
LVL 12

Accepted Solution

by:
Ivanov_G earned 900 total points
ID: 11939651
> Now obviously when a user selects "OK" the settings are saved and the settings form is closed. No Prob.
> And when a User "Cancels", no settings are saved and the old settings should be reloaded and then the settings form is closed. No Prob.
> "Apply" is the same as "OK" except no settings are saved.

OK - Save settings and close the form
Apply - Save settings without closing the form
Cancel - Do NOT save settings and close the form

> Now the problem I have, is when and where do I load the settings when my application is started!
> When I load the settings at startup, I cant set any of the components in my settings form unless my settings form is visible.
> I thought all forms are created right away when an app is created?
> Shouldnt you be able to change things on a form even if its hidden?

you can do it like this - implement a class with the settings, for e.g.

TMySettings = class(TObject)
private
public
  DBAlias     : String;
  DBUsername : String;
  procedure SaveToIni (FileName : String);
  procedure LoadFromIni (FileName : String);
end;

etc. OnCreate of your FIRST form create and instance of this class and load the settings from file/registry/"or whatever you want". Then when open the Options form you can access the information from your class...
0
 
LVL 4

Author Comment

by:LMuadDIb
ID: 11948502
I thought "Apply" was only to test settings, and you still had a chance to "Cancel" all settings if you didnt like them.
So I thought you shouldnt save the settings after a user clicks "Apply", only on "OK".

global vars/object is the way to go then, thanx :)
0
 
LVL 4

Author Comment

by:LMuadDIb
ID: 11948526
crap! :(
I forgot to accept Ivanov_G answer before I increased the pointsto split with the others
was going to give 125 (original pts for the question) to Ivanov_G, 75 pts to geobul & esoftbg would have gotten 25pts
sorry, screwd up... maybe a mod can fix

0

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question