Link to home
Start Free TrialLog in
Avatar of Peter Kiers
Peter KiersFlag for Netherlands

asked on

Question about inifile

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

SOLUTION
Avatar of rfwoolf
rfwoolf
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Peter Kiers

ASKER

Still get the same errors...
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of piba
piba

also for the writing part
 ini.WriteString('FONT', 'FaceName', cbFontName.Text);
 ini.WriteInteger('FONT', 'PointSize', strtointdef(edtFontSize.Text,12));
I get:
[Pascal Error] Settings.pas(108): Undeclared identifier: Index
[Pascal Error] Settings.pas(108): E2010 Incompatible types: 'Integer' and 'String'
change Index into ItemIndex
Still doesn't work
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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

for load section try
    cbFontName.itemindex := cbFontName.IndexOf(ini.ReadString('FONT', 'FaceName', 'Tahoma'));
    edtFontSize.text := ini.ReadInteger('FONT', 'PointSize','12');
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
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;
I get incompatible types: string and integer

p.
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.
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;

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;
if your having an advance work in .ini files, you can read information from that accepted link;
https://www.experts-exchange.com/questions/26322686/Wor-with-ini-file.html
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;
Can someone help me?
>> // 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);
I am helping to close this post, as you noticed my response are all positive, step by step.
There are no more errors.
but when I choose a fontsize it will be written to inifile
but not loaded.
 [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
"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
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);

try those lines above;
edtFontSize.Text ? is a TEdit?
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

How is it now?
I give up. Because I have got a right solution, i will devide the points to everyone
who responded.

Greetings, Peter Kiers
I have receive no good solution.
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.