Solved

Question about inifile

Posted on 2010-08-15
32
745 Views
Last Modified: 2012-05-10
Hi,

I have a main form called MainForm and a dialogform called frmSettings.
The frmSettings can be called by menu-item Settings. On the frmSettings
I have a combobox called cbFontName for choosing a fontname and an
Edit component called edtFontSize for choosing  the fontsize. The fontname
and size will be passed to a dbrichedit on the mainform.

My question is: when the frmSettings is closed the status of the combobox
and edit component must be written to an inifile.  And if the frmSettings is
called the status of the combobox and edit component must be loaded.

This is what I have:

procedure TfrmSettings.LoadSettings;
var ini: TIniFile;
begin
  ini := TIniFile.Create(fSettingsFile);
  try
    cbFontName := ini.ReadString('FONT', 'FaceName', cbFontName);
    edtFontSize := ini.ReadInteger('FONT', 'PointSize', edtFontSize);
  finally
    FreeAndNil(ini)
  end;
end;
(*---------------------------------------------------*)
procedure TfrmSettings.SaveSettings;
var ini: TIniFile;
begin
  ini := TIniFile.Create(fSettingsFile);
  try
    ini.WriteString('FONT', 'FaceName', cbFontName);
    ini.WriteInteger('FONT', 'PointSize', edtFontSize);
  finally
    FreeAndNil(ini)
  end;
end;

But I get errors:
[Pascal Error] Settings.pas(95): E2010 Incompatible types: 'string' and 'TComboBox'
[Pascal Error] Settings.pas(96): E2010 Incompatible types: 'Integer' and 'TEdit'
[Pascal Error] Settings.pas(107): E2010 Incompatible types: 'string' and 'TComboBox'
[Pascal Error] Settings.pas(108): E2010 Incompatible types: 'Integer' and 'TEdit'

Who can help me?
I have put all the code of the frmSettings in the code-section.

Peter


function EnumFontsProc(var LogFont: TLogFont; var TextMetric: TTextMetric;

  FontType: Integer; Data: Pointer): Integer; stdcall;

begin

  TStrings(Data).Add(LogFont.lfFaceName);

  Result := 1;

end;

(*---------------------------------------------------*)

procedure TfrmSettings.cbFontNameChange(Sender: TObject);

begin

  CurrText.Name := cbFontName.Items[cbFontName.ItemIndex];

end;

(*---------------------------------------------------*)

function TfrmSettings.CurrText: TTextAttributes;

begin

  if mainform.dbrichedit1.SelLength > 0 then Result := mainform.dbrichedit1.SelAttributes

  else Result := mainform.dbrichedit1.DefAttributes;

end;

(*---------------------------------------------------*)

procedure TfrmSettings.FontSizeChange(Sender: TObject);

begin

  CurrText.Size := StrToInt(edtFontSize.Text);

end;

(*---------------------------------------------------*)

procedure TfrmSettings.FormCreate(Sender: TObject);

begin

 GetFontNames;     //voer proc. GetFontNames uit

   CurrText.Name := DefFontData.Name;

  CurrText.Size := -MulDiv(DefFontData.Height, 72, Screen.PixelsPerInch);

 //maak een .ini-file aan in je applicatie met de naam van je applicatie

 //en laad deze ini de string fsettingsfile

 fSettingsFile := ChangeFileExt(Application.ExeName, '.ini');

 LoadSettings;     //voer proc. LoadSettings uit

end;

(*---------------------------------------------------*)

procedure TfrmSettings.GetFontNames;

var

  DC: HDC;

begin

  DC := GetDC(0);

  EnumFonts(DC, nil, @EnumFontsProc, Pointer(cbFontName.Items));

  ReleaseDC(0, DC);

  cbFontName.Sorted := True;

end;

(*---------------------------------------------------*)

procedure TfrmSettings.LoadSettings;

var ini: TIniFile;

begin

  ini := TIniFile.Create(fSettingsFile);

  try

    cbFontName := ini.ReadString('FONT', 'FaceName', cbFontName);

    edtFontSize := ini.ReadInteger('FONT', 'PointSize', edtFontSize);

  finally

    FreeAndNil(ini)

  end;

end;

(*---------------------------------------------------*)

procedure TfrmSettings.SaveSettings;

var ini: TIniFile;

begin

  ini := TIniFile.Create(fSettingsFile);

  try

    ini.WriteString('FONT', 'FaceName', cbFontName);

    ini.WriteInteger('FONT', 'PointSize', edtFontSize);

  finally

    FreeAndNil(ini)

  end;

end;

(*---------------------------------------------------*)

end.

Open in new window

0
Comment
Question by:peterkiers
  • 14
  • 12
  • 3
  • +1
32 Comments
 
LVL 13

Assisted Solution

by:rfwoolf
rfwoolf earned 100 total points
ID: 33439765
Double-check your syntax for ini.ReadInteger and ini.ReadString, I am sure that the 3rd parameter is your 'default' value in case the INI file does not return a value, so try these:
 cbFontName := ini.ReadString('FONT', 'FaceName', 'Tahoma');
 edtFontSize := ini.ReadInteger('FONT', 'PointSize', '12');
0
 
LVL 1

Author Comment

by:peterkiers
ID: 33439775
Still get the same errors...
0
 
LVL 3

Assisted Solution

by:piba
piba earned 150 total points
ID: 33439776
you should also not set the read values directly into the combobox/textbox object reference but into its properties:

cbFontName.index :=   cbFontName.items.indexof(ini.ReadString('FONT', 'FaceName', 'Tahoma'));
edtFontSize.Text := inttostr(ini.ReadInteger('FONT', 'PointSize', '12'));
0
 
LVL 3

Expert Comment

by:piba
ID: 33439791
also for the writing part
 ini.WriteString('FONT', 'FaceName', cbFontName.Text);
 ini.WriteInteger('FONT', 'PointSize', strtointdef(edtFontSize.Text,12));
0
 
LVL 1

Author Comment

by:peterkiers
ID: 33439799
I get:
[Pascal Error] Settings.pas(108): Undeclared identifier: Index
[Pascal Error] Settings.pas(108): E2010 Incompatible types: 'Integer' and 'String'
0
 
LVL 1

Author Comment

by:peterkiers
ID: 33439802
change Index into ItemIndex
0
 
LVL 1

Author Comment

by:peterkiers
ID: 33439833
Still doesn't work
0
 
LVL 14

Accepted Solution

by:
systan earned 250 total points
ID: 33439931
You recieved an error;
[Pascal Error] Settings.pas(95): E2010 Incompatible types: 'string' and 'TComboBox'
[Pascal Error] Settings.pas(96): E2010 Incompatible types: 'Integer' and 'TEdit'
[Pascal Error] Settings.pas(107): E2010 Incompatible types: 'string' and 'TComboBox'
[Pascal Error] Settings.pas(108): E2010 Incompatible types: 'Integer' and 'TEdit'

Because you did not complete the properties you wanted to use.

Look at the updated code;
procedure TfrmSettings.LoadSettings;

var ini: TIniFile;

begin

  ini := TIniFile.Create(fSettingsFile);

  try

    cbFontName := ini.ReadString('FONT', 'FaceName', cbFontName.items[cbFontName.ItemIndex]);

    edtFontSize := ini.ReadInteger('FONT', 'PointSize', edtFontSize.Text);

  finally

    FreeAndNil(ini)

  end;

end;

(*---------------------------------------------------*)

procedure TfrmSettings.SaveSettings;

var ini: TIniFile;

begin

  ini := TIniFile.Create(fSettingsFile);

  try

    ini.WriteString('FONT', 'FaceName', cbFontName.items[cbFontName.ItemIndex]);

    ini.WriteInteger('FONT', 'PointSize', edtFontSize.Text);

  finally

    FreeAndNil(ini)

  end;

end;

Open in new window

0
 
LVL 14

Expert Comment

by:systan
ID: 33439942
Ops;
LoadSettings Correction;
procedure TfrmSettings.LoadSettings;

var ini: TIniFile;

begin

  ini := TIniFile.Create(fSettingsFile);

  try

    cbFontName := ini.ReadString('FONT', 'FaceName', '');

    edtFontSize := ini.ReadInteger('FONT', 'PointSize','');

  finally

    FreeAndNil(ini)

  end;

end;

Open in new window

0
 
LVL 13

Expert Comment

by:rfwoolf
ID: 33439957
for load section try
    cbFontName.itemindex := cbFontName.IndexOf(ini.ReadString('FONT', 'FaceName', 'Tahoma'));
    edtFontSize.text := ini.ReadInteger('FONT', 'PointSize','12');
0
 
LVL 13

Expert Comment

by:rfwoolf
ID: 33439972
oh I see, if you're using Systan's write method then it saves the itemindex (which if you ask me might not be the best way because if the user adds more fonts, then the itemindex of 'tahoma' might be different, for example), but if that's what you want, then it's more simple:
  cbFontName.itemindex := inttostr(ini.ReadString('FONT', 'FaceName', '11'));
(and instead of calling it 'FaceName' maybe call it FaceNameIndex
0
 
LVL 1

Author Comment

by:peterkiers
ID: 33439985
only the lines that are commented are not working:

procedure TfrmSettings.LoadSettings;
var ini: TIniFile;
begin
  ini := TIniFile.Create(fSettingsFile);
  try
    cbFontName.Text := ini.ReadString('FONT', 'FaceName', '');
  //  edtFontSize.Text := ini.ReadInteger('FONT', 'PointSize', '');  <=============
  finally
    FreeAndNil(ini)
  end;
end;
(*---------------------------------------------------*)
procedure TfrmSettings.SaveSettings;
var ini: TIniFile;
begin
  ini := TIniFile.Create(fSettingsFile);
  try
    ini.WriteString('FONT', 'FaceName', cbFontName.Text);
  //  ini.WriteInteger('FONT', 'PointSize', edtFontSize.Text);  <=============
  finally
    FreeAndNil(ini)
  end;
end;
0
 
LVL 1

Author Comment

by:peterkiers
ID: 33440000
I get incompatible types: string and integer

p.
0
 
LVL 14

Expert Comment

by:systan
ID: 33440052
cbFontName? is a combobox?
if not, use .Text
Ok, I see you got it now on
ini.WriteString('FONT', 'FaceName', cbFontName.Text);

But in
//  edtFontSize.Text := ini.ReadInteger('FONT', 'PointSize', '');  <=============
//  ini.WriteInteger('FONT', 'PointSize', edtFontSize.Text);  <=============

Why not use
edtFontSize.Text := ini.ReadString('FONT', 'PointSize', '');
ini.WriteString('FONT', 'PointSize', edtFontSize.Text);
Anyway it is always a number that the input is.
And even if you input a text instead of a number, and go, no problem about the inputs.
0
 
LVL 1

Author Comment

by:peterkiers
ID: 33440068
I get econvertererror: is not an integer value:

procedure TfrmSettings.LoadSettings;
var ini: TIniFile;
begin
  ini := TIniFile.Create(fSettingsFile);
  try
    cbFontName.Text := ini.ReadString('FONT', 'FaceName', '');
    edtFontSize.Text := ini.ReadString('FONT', 'PointSize', '');
  finally
    FreeAndNil(ini)
  end;
end;
(*---------------------------------------------------*)
procedure TfrmSettings.SaveSettings;
var ini: TIniFile;
begin
  ini := TIniFile.Create(fSettingsFile);
  try
    ini.WriteString('FONT', 'FaceName', cbFontName.Text);
    ini.WriteString('FONT', 'PointSize', edtFontSize.Text);
  finally
    FreeAndNil(ini)
  end;
end;

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 14

Expert Comment

by:systan
ID: 33440073
But if you insist to input or use ReadInteger, Ok;
from
//  edtFontSize.Text := ini.ReadInteger('FONT', 'PointSize', '');  <=============
//  ini.WriteInteger('FONT', 'PointSize', edtFontSize.Text);  <=============

to
var intvalue:integer;
ini.WriteInteger('FONT', 'PointSize', strtoint( edtFontSize.Text) );
intvalue := ini.ReadInteger('FONT', 'PointSize', '')
edtFontSize.Text := intvalue;
0
 
LVL 14

Expert Comment

by:systan
ID: 33440085
if your having an advance work in .ini files, you can read information from that accepted link;
http://www.experts-exchange.com/Programming/Languages/Pascal/Delphi/Q_26322686.html
0
 
LVL 1

Author Comment

by:peterkiers
ID: 33440093
the lines that re commented are not working:

procedure TfrmSettings.LoadSettings;
var ini: TIniFile;
    intvalue:integer;
begin
  ini := TIniFile.Create(fSettingsFile);
  try
    cbFontName.Text := ini.ReadString('FONT', 'FaceName', '');
   // intvalue := ini.ReadInteger('FONT', 'PointSize', '');   <=========
   // edtFontSize.Text := intvalue;     <=========
  finally
    FreeAndNil(ini)
  end;
end;
(*---------------------------------------------------*)
procedure TfrmSettings.SaveSettings;
var ini: TIniFile;
begin
  ini := TIniFile.Create(fSettingsFile);
  try
    ini.WriteString('FONT', 'FaceName', cbFontName.Text);
    ini.WriteInteger('FONT', 'PointSize', strtoint(edtFontSize.Text) );
  finally
    FreeAndNil(ini)
  end;
end;
0
 
LVL 1

Author Comment

by:peterkiers
ID: 33440125
Can someone help me?
0
 
LVL 14

Expert Comment

by:systan
ID: 33440137
>> // intvalue := ini.ReadInteger('FONT', 'PointSize', '');   <=========
>>   // edtFontSize.Text := intvalue;     <=========

Just as I suspected;
add this;
intvalue := ini.ReadInteger('FONT', 'PointSize', 0);
or
intvalue := ini.ReadInteger('FONT', 'PointSize', intvalue);

edtFontSize.Text :=inttostr(intvalue);
0
 
LVL 14

Expert Comment

by:systan
ID: 33440140
I am helping to close this post, as you noticed my response are all positive, step by step.
0
 
LVL 1

Author Comment

by:peterkiers
ID: 33440151
There are no more errors.
but when I choose a fontsize it will be written to inifile
but not loaded.
0
 
LVL 1

Author Comment

by:peterkiers
ID: 33440154
 [Pascal Hint] Settings.pas(104): H2077 Value assigned to 'intvalue' never used
  [Pascal Warning] Settings.pas(102): W1036 Variable 'intvalue' might not have been initialized
0
 
LVL 1

Author Comment

by:peterkiers
ID: 33440173
"I am helping to close this post, as you noticed my response are all positive, step by step."

The points are all yours if you give me a working solution.
I'll be back in a few hours...

Greetings,

Peter Kiers
0
 
LVL 14

Expert Comment

by:systan
ID: 33440178
edtFontSize.Text ? is a TEdit?
Ok, try those following lines;

intvalue := ini.ReadInteger('FONT', 'PointSize', integer);

or
var intvalue:integer=0; variable value declared
intvalue := ini.ReadInteger('FONT', 'PointSize', intvalue); //again
edtFontSize.Text :=inttostr(intvalue);

or
var intvalue:integer=0; variable value declared
var defaultvalue: integer;
intvalue := ini.ReadInteger('FONT', 'PointSize', defaultvalue);
edtFontSize.Text :=inttostr(intvalue);

0
 
LVL 14

Expert Comment

by:systan
ID: 33440188
try those lines above;
edtFontSize.Text ? is a TEdit?
0
 
LVL 14

Expert Comment

by:systan
ID: 33440204
Please compare your code structure if it follows the same as the code below;
///////////////////begin unit1.pas

unit Unit1;



interface



uses

  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,

  Dialogs, StdCtrls, Menus, DB, DBClient, Provider, ComCtrls, IniFiles;



type

  TForm1 = class(TForm)

    Edit1: TEdit;

    Edit2: TEdit;



    RadioButton1: TRadioButton;

    RadioButton2: TRadioButton;



    CheckBox1: TCheckBox;

    CheckBox2: TCheckBox;

    CheckBox3: TCheckBox;



    TrackBar1: TTrackBar;



    MainMenu1: TMainMenu;

    File1: TMenuItem;

    Save1: TMenuItem;

    Open1: TMenuItem;



    procedure Save1Click(Sender: TObject);

    procedure Open1Click(Sender: TObject);

  private

    { Private declarations }

  public

    { Public declarations }

  end;



var

  Form1: TForm1;

  Inifile : tinifile;

implementation



{$R *.dfm}





procedure TForm1.Save1Click(Sender: TObject);

begin

IniFile:=Tinifile.Create(ExtractFilePath(Application.ExeName)+'\myproject.mpj');

inifile.WriteBool('Checkboxes','CheckBox1',Checkbox1.Checked);

inifile.WriteBool('Checkboxes','CheckBox2',Checkbox2.Checked);

inifile.WriteBool('Checkboxes','CheckBox3',Checkbox3.Checked);

inifile.WriteBool('RadioButton','RadioButton1',RadioButton1.Checked);

inifile.WriteBool('RadioButton','RadioButton2',RadioButton2.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.Open1Click(Sender: TObject);

begin

IniFile:=Tinifile.Create(ExtractFilePath(Application.ExeName)+'\myproject.mpj');

Checkbox1.Checked := Inifile.ReadBool('Checkboxes','Checkbox1',False);

Checkbox2.Checked := Inifile.ReadBool('Checkboxes','Checkbox2',False);

Checkbox3.Checked := Inifile.ReadBool('Checkboxes','Checkbox3',False);

RadioButton1.Checked := Inifile.ReadBool('RadioButton','RadioButton1',False);

RadioButton2.Checked := Inifile.ReadBool('RadioButton','RadioButton2',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.

///////////////////end unit1.pas





{

///////////////////begin .DFM

object Form1: TForm1

  Left = 394

  Top = 210

  BorderStyle = bsSingle

  Caption = 'Form1'

  ClientHeight = 381

  ClientWidth = 230

  Color = clBtnFace

  Font.Charset = DEFAULT_CHARSET

  Font.Color = clWindowText

  Font.Height = -11

  Font.Name = 'Tahoma'

  Font.Style = []

  Menu = MainMenu1

  OldCreateOrder = False

  PixelsPerInch = 96

  TextHeight = 13

  object Edit1: TEdit

    Left = 56

    Top = 8

    Width = 121

    Height = 21

    TabOrder = 0

    Text = 'Edit1'

  end

  object RadioButton1: TRadioButton

    Left = 56

    Top = 80

    Width = 97

    Height = 25

    Caption = 'RadioButton1'

    TabOrder = 1

  end

  object RadioButton2: TRadioButton

    Left = 56

    Top = 112

    Width = 97

    Height = 17

    Caption = 'RadioButton2'

    TabOrder = 2

  end

  object CheckBox1: TCheckBox

    Left = 56

    Top = 144

    Width = 97

    Height = 17

    Caption = 'CheckBox1'

    TabOrder = 3

  end

  object CheckBox2: TCheckBox

    Left = 56

    Top = 176

    Width = 97

    Height = 17

    Caption = 'CheckBox2'

    TabOrder = 4

  end

  object CheckBox3: TCheckBox

    Left = 56

    Top = 208

    Width = 97

    Height = 17

    Caption = 'CheckBox3'

    TabOrder = 5

  end

  object Edit2: TEdit

    Left = 56

    Top = 40

    Width = 121

    Height = 21

    TabOrder = 6

    Text = 'Edit2'

  end

  object TrackBar1: TTrackBar

    Left = 40

    Top = 248

    Width = 150

    Height = 45

    TabOrder = 7

  end

  object MainMenu1: TMainMenu

    object File1: TMenuItem

      Caption = 'File'

      object Save1: TMenuItem

        Caption = 'Save'

        OnClick = Save1Click

      end

      object Open1: TMenuItem

        Caption = 'Open'

        OnClick = Open1Click

      end

    end

  end

end

///////////////////end .DFM

}

Open in new window

0
 
LVL 14

Expert Comment

by:systan
ID: 33440640
How is it now?
0
 
LVL 1

Author Comment

by:peterkiers
ID: 33440766
I give up. Because I have got a right solution, i will devide the points to everyone
who responded.

Greetings, Peter Kiers
0
 
LVL 1

Author Closing Comment

by:peterkiers
ID: 33440774
I have receive no good solution.
0
 
LVL 14

Expert Comment

by:systan
ID: 33440791
C? Rating 1.0?
With the sample code's I gave and the step by step program troubleshooting solution,  I guess it's right for you but not for us.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

863 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

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now