Solved

Using a prefernce/options form

Posted on 2004-08-30
5
124 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
Comment Utility
> "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
Comment Utility
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
Comment Utility
> 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
Comment Utility
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
Comment Utility
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

772 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now