?
Solved

Saving settings...

Posted on 2003-03-16
3
Medium Priority
?
145 Views
Last Modified: 2010-04-04
Hi all again,

I have another problem, I'm creating a program for viewing nfo files, it's almost done, but here's my real question...

When I enter the program it's using the default font (Lucida Console), font color(White) and background color(Black), but if I change the font to sans serif, the font color to green and the background color to blue, if I close the program and re-open it won't save the settings and it goes back to default...

So, is there a way to save these settings whenever I change them? Save to a file or something?

Thanks for any replys, I'm giving all my points here (55 =/), hope it's enough...
0
Comment
Question by:zicodt
[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
3 Comments
 
LVL 17

Expert Comment

by:geobul
ID: 8150146
Hi,

You may use the registry to save your settings (under your app specific key) or you may use ini file saved in your exe folder. When your program statrs, read the values and use them. When you change these settings, save the new values back in the registry (or ini file).

Registry example:

uses Registry;

var
  fnt: string;
  clr: TColor;

procedure ReadSettings;
begin
  with TRegistry.Create do begin
    if OpenKey('Software\YourApp', false) then begin
      fnt := ReadString('Font');
      clr := ReadInteger('Color');
      CloseKey;
    end;
    Free;
  end;
end;

procedure SaveSettings;
begin
  with TRegistry.Create do begin
    if OpenKey('Software\YourApp', true) then begin
      WriteString('Font', fnt);
      WriteInteger('Color', clr);
      CloseKey;
    end;
    Free;
  end;
end;

Regards, Geo
0
 
LVL 7

Accepted Solution

by:
Motaz earned 220 total points
ID: 8150252
Another solution is to use Ini Files:


Application settings and premenent variables are always stored in text files. It is the easiest way. Text files lack the ability of direct access, instead you have to read all the lines to access certain information. Also it is very hard in modification, for example if you want to insert a line in a text file, you should create temporary file and copy all data to it.

Delphi and Windows provides a very good and easy technique to store such configuration and setting data. It is the INI files.
Ini files is text files, but it is very organized, it is consists of sections, each sections contains a identifiers and values. You can access any section, and identifier directly without wory about the preceding and subsequent data lines.

Example of Ini file structure:

[windows]
load=
run=
NullPort=None
BaseCodePage=1256
BaseCodePage=1256

[Desktop]
Wallpaper=C:\WINDOWS\SETUP.BMP
TileWallpaper=0
WallpaperStyle=0

[Intl]
iCountry=966
ICurrDigits=2
iCurrency=2
iDate=1

Example of section names: [Windows], [Desktop].
Example of identifiers (Variables) : run, load, iDate
Example of values: 966, 2, C:\WINDOWS\SETUP.BMP

Writing data to Ini file example:

var
  Ini: TIniFile;
begin
  Ini:= TIniFile.Create('settings.ini');
  Ini.WriteString('Form', 'caption', Form1.Caption);
  Ini.WriteInteger('Form', 'left', Form1.Left);
  Ini.WriteInteger('Form', 'top', Form1.Top);
  Ini.WriteInteger('Form', 'width', Form1.Width);
  Ini.WriteInteger('Form', 'height', Form1.Height);
  Ini.Free;
end;

Reading data from Ini file example:

var
  Ini: TIniFile;
begin
  Ini:= TIniFile.Create('settings.ini');
  Form1.Caption:= Ini.ReadString('Form', 'caption', Form1.Caption);
  Form1.Left:= Ini.ReadInteger('Form', 'left', Form1.Left);
  Form1.Top:= Ini.ReadInteger('Form', 'top', Form1.Top);
  Form1.Width:= Ini.ReadInteger('Form', 'width', Form1.Width);
  Form1.Height:= Ini.ReadInteger('Form', 'height', Form1.Height);
  Ini.Free;
end;


Notes:

- TIniFile class exists in IniFiles unit, so that you need to add IniFiles to your uses clause.
- In Reading values, you can assign a default value in case of the value, section, or the ini file does not exist, example:

Speed:= Ini.ReadString('car', 'speed', 220);.

In this case if 'speed' does not assigned yet, then 220 will be returned.
- Not like Windows Registry, which it is a centralized database, INI files can be stored in any directory, such as applications working directory, so that it is suitable when you want to have multiple executables for the same version of your application in different directories to have different settings.

Motaz
0
 
LVL 1

Author Comment

by:zicodt
ID: 8153241
Cool...

I liked both answers, but I'll stick with motaz answer cause I want to have two instances of the program running with different settings...

But geobul I appreciate alot your reply, it helped me also...

Thanks both of you...
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
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…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
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