Reading and writing Ini files?

I'm a Visual Basic programmer that is just beginning to understand the power of delphi.  I am new to delphi and was wondering if someone could tell me (if possible) how to read and write to ini files.
LVL 3
fibdevAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

intheCommented:
Hi,
the following example will save the 1:some boolean values (the state of checkboxes)
3:
some string values(the text from a edit box)
3:
an integer value(the trackbars position)

try it see if you understand,and if you need any more help just shout
Regards Barry

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls,IniFiles, ComCtrls;

type
  TForm1 = class(TForm)
    CheckBox1: TCheckBox;
    CheckBox2: TCheckBox;
    CheckBox3: TCheckBox;
    Edit1: TEdit;
    TrackBar1: TTrackBar;
    Edit2: TEdit;
    procedure FormDestroy(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  IniFile : TIniFile;
implementation

{$R *.DFM}

procedure TForm1.FormDestroy(Sender: TObject);
begin
IniFile:=Tinifile.Create('c:\myini.ini');
inifile.WriteBool('Checkboxes','CheckBox1',Checkbox1.Checked);
inifile.WriteBool('Checkboxes','CheckBox2',Checkbox2.Checked);
inifile.WriteBool('Checkboxes','CheckBox3',Checkbox3.Checked);
IniFile.WriteString('Edits_Text','Edit1',Edit1.Text);
IniFile.WriteString('Edits_Text','Edit2',Edit2.Text);
IniFile.WriteInteger('TrackBars','TrackBar1',Trackbar1.Position);
InIfile.Free;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
IniFile:=Tinifile.Create('c:\myini.ini');
Checkbox1.Checked := Inifile.ReadBool('Checkboxes','Checkbox1',False);
Checkbox2.Checked := Inifile.ReadBool('Checkboxes','Checkbox2',False);
Checkbox3.Checked := Inifile.ReadBool('Checkboxes','Checkbox3',False);
Edit1.Text := IniFile.ReadString('Edits_Text','Edit1','');
Edit2.Text := IniFile.ReadString('Edits_Text','Edit2','');
TrackBar1.Position := IniFile.ReadInteger('TrackBars','TrackBar1',0);
IniFile.Free;
end;

end.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
intheCommented:
another quick example:
this saves the values of a listbox to an ini file and reads them back into the listbox on form create:
also forgot to mention you need to add "inifiles" to the uses section to work with ini files...


unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls,inifiles;

type
  TForm1 = class(TForm)
    Button1: TButton;
    ListBox1: TListBox;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  Inifile : tinifile;
  ii : integer;
  implementation

uses Unit2;

{$R *.DFM}


procedure TForm1.FormCreate(Sender: TObject);
var
i : integer;
s : string;
sl : tstringlist;
begin
if fileexists('c:\mylist.ini') then begin
sl := tstringlist.create;
sl.loadfromfile('c:\mylist.ini');
for i := 1 to sl.count -1  do begin
S:= sl.Strings[i];
Listbox1.Items.add(Copy(S, Pos('=',S)+1, maxint));
end; //for
sl.free;
end;//fileexist

end;

procedure TForm1.FormDestroy(Sender: TObject);
var
 i : integer;
begin
IniFile:=Tinifile.Create('c:\mylist.ini');
for i := 0 to listbox1.Items.Count -1
do begin
IniFile.WriteString('listitems','item'+inttostr(i),listbox1.Items[i]);
end;
InIfile.Free;
end;



procedure TForm1.Button1Click(Sender: TObject);
begin
//for testing
listbox1.Items.Loadfromfile('c:\autoexec.bat');
end;

end.
0
fibdevAuthor Commented:
Thanks!

How do I hide the form when so the user can't see it at all when it loads?

any extra help would be appreciated.
0
intheCommented:
hi,
just call first (either on form activate event or form create event).

Form1.visible := false;
then when the ini is loaded call
Form1.visible := true;

ps you are making a wise move by learning delphi ,alot of vb programmers lik the extra power delphi brings  ;-)
good luck
Regards Barry
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.