Solved

Using a prefernce/options form

Posted on 2004-08-30
5
127 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
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 225 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

828 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