Solved

Using a prefernce/options form

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to open a new windows chrome resized and disabled the toolbar? 3 113
CheckListBox usage 3 58
Firemonkey DbLookupComboBox equivalent ? 2 47
DBGrid or StringGrid ? 6 88
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

776 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