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

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 758
  • Last Modified:

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

0
peterkiers
Asked:
peterkiers
  • 14
  • 12
  • 3
  • +1
3 Solutions
 
rfwoolfCommented:
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
 
peterkiersAuthor Commented:
Still get the same errors...
0
 
pibaCommented:
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
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!

 
pibaCommented:
also for the writing part
 ini.WriteString('FONT', 'FaceName', cbFontName.Text);
 ini.WriteInteger('FONT', 'PointSize', strtointdef(edtFontSize.Text,12));
0
 
peterkiersAuthor Commented:
I get:
[Pascal Error] Settings.pas(108): Undeclared identifier: Index
[Pascal Error] Settings.pas(108): E2010 Incompatible types: 'Integer' and 'String'
0
 
peterkiersAuthor Commented:
change Index into ItemIndex
0
 
peterkiersAuthor Commented:
Still doesn't work
0
 
systanCommented:
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
 
systanCommented:
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
 
rfwoolfCommented:
for load section try
    cbFontName.itemindex := cbFontName.IndexOf(ini.ReadString('FONT', 'FaceName', 'Tahoma'));
    edtFontSize.text := ini.ReadInteger('FONT', 'PointSize','12');
0
 
rfwoolfCommented:
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
 
peterkiersAuthor Commented:
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
 
peterkiersAuthor Commented:
I get incompatible types: string and integer

p.
0
 
systanCommented:
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
 
peterkiersAuthor Commented:
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
 
systanCommented:
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
 
systanCommented:
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
 
peterkiersAuthor Commented:
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
 
peterkiersAuthor Commented:
Can someone help me?
0
 
systanCommented:
>> // 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
 
systanCommented:
I am helping to close this post, as you noticed my response are all positive, step by step.
0
 
peterkiersAuthor Commented:
There are no more errors.
but when I choose a fontsize it will be written to inifile
but not loaded.
0
 
peterkiersAuthor Commented:
 [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
 
peterkiersAuthor Commented:
"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
 
systanCommented:
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
 
systanCommented:
try those lines above;
edtFontSize.Text ? is a TEdit?
0
 
systanCommented:
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
 
systanCommented:
How is it now?
0
 
peterkiersAuthor Commented:
I give up. Because I have got a right solution, i will devide the points to everyone
who responded.

Greetings, Peter Kiers
0
 
peterkiersAuthor Commented:
I have receive no good solution.
0
 
systanCommented:
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

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

  • 14
  • 12
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now