Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Saving settings...

Posted on 2003-03-16
3
Medium Priority
?
146 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
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

Independent Software Vendors: 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

Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
This is an update to some code that someone else posted on Experts Exchange. It is an alternate approach, I think a little easier to use, & makes sure that things like the Task Bar will update.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Loops Section Overview
Suggested Courses

581 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