?
Solved

Using a prefernce/options form

Posted on 2004-08-30
5
Medium Priority
?
134 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
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…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

770 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