Delphi-PRAXiS
Seite 1 von 2  1 2      

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Object-Pascal / Delphi-Language (https://www.delphipraxis.net/32-object-pascal-delphi-language/)
-   -   Delphi In English (Ini question) (https://www.delphipraxis.net/36809-english-ini-question.html)

jcop 26. Dez 2004 12:05


In English (Ini question)
 
Hello,

Sorry i write this in english (I can read and understand german, just no write correctly)

I would like to read some values from a ini file, but not the sections, some lines in the title.
EG

[Section.1]
Title= Nissan
colour= Red

[Section.2]
Title= Renault
colour= Blue

etc...

I just want a combobox showing all titles from this ini file

Nissan
Renault
etc...

I know you can do that with readscetions, but then it's reading all [] and thats not the result what I want cause I also have a section

[general]
settings=56

and this may not be shown in the combobox or listbox.

hope you can assist me.
Merry X-mass to all of you.

Fubar 26. Dez 2004 12:14

Re: In English (Ini question)
 
Du kannst doch "ReadSections" verwenden und anschliessend aus jeder Section den Eintrag "Title" herauslesen.

jcop 26. Dez 2004 12:19

Re: In English (Ini question)
 
Yes I know how to perform the readsections, but I don't know how extract the title's, should I create a streamtype or array for those?
Thanks for the quick reply

Elite 26. Dez 2004 12:29

Re: In English (Ini question)
 
Nehm doch die Funktion ReadString(const Section, Ident, Default: String): String;
So benutzt du die:
Code:
MyTitleVar := MyIni.ReadString('Section.1', 'Title', 'TheDefaultTitle');

Christian Seehase 26. Dez 2004 13:13

Re: In English (Ini question)
 
Moin Jcop,

Welcome to Delphi-PRAXiS.

First you can read all sectionnames into a TStringList using ReadSections.
Then you remove the fixed section names.
Now you can loop through the TStringList and read all title entries to add them to the TComboBox.

Sprint 26. Dez 2004 13:51

Re: In English (Ini question)
 
Delphi-Quellcode:
procedure TForm1.Button1Click(Sender: TObject);
const
  S_TITLE = 'Title';
var
  Sections: TStringList;
  I: Integer;
begin

  Sections := TStringList.Create;
  try
    IniFile.ReadSections(Sections);
    for I := 0 to Sections.Count - 1 do
    begin
      if IniFile.ValueExists(Sections[I], S_TITLE) then
        ComboBox1.Items.Add(IniFile.ReadString(Sections[I], S_TITLE, ''));
    end;
  finally
    Sections.Free;
  end;

end;

jcop 26. Dez 2004 14:31

Re: In English (Ini question)
 
Thanks, seems to be working, danke sehr, dank U, merci etc.... :firejump: :dancer2: :witch: :dancer: :bounce1: :bouncing4: :hello: :chat: :hi: :spin:

Christian Seehase 26. Dez 2004 15:48

Re: In English (Ini question)
 
Moin Sprint,

gute Idee, so geht's natürlich noch besser. :thumb:
(vorausgesetzt, dass es den Wertnamen nicht noch in einer anderen Section gibt)

jcop 27. Dez 2004 14:48

Re: In English (Ini question)
 
Hello,

Again a question concerning this method.

[Section.1]
Title= Nissan
colour= Red
engine= 1400cc
Turbo = no

[Section.2]
Title= Renault
colour= Blue
engine = 1800
Turbo= yes

Now I can see the titles inside de combobox, but when I click on 1 of them to view the colour for eg.
my dedicated textbox stays empty.
I was using the following method for this function but then again this was only working when loading the sections into the combobox.
Delphi-Quellcode:
procedure TForm1.cboSectionsChange(Sender: TObject);
begin
  carconfig := Tinifile.Create(opendialog1.FileName);

  with carconfig do
  try
  ReadSections(CboSections.Items);

  txtTitle.Text := Readstring(cboSections.Text, 'Title', '');
  txtColour.Text := ReadString (cboSections.Text, 'Colour', '');
  txtEngine.Text := ReadString (cboSections.Text, 'engine', '');
  txtTurbo.Text := ReadString (cboSections.Text, 'Turbo','');

  Finally
  carconfig.Free;
  end;
end;
I was thinking of porting the button1.openclick event also into the cbosctionschange event but that's resulting in a error.

Hope you can help me this one also, providing me with the right direction to search would also be welcome.
Thanks

Sprint 27. Dez 2004 15:48

Re: In English (Ini question)
 
Zitat:

Zitat von jcop
I was thinking of porting the button1.openclick event also into the cbosctionschange event but that's resulting in a error.

Delphi-Quellcode:
type
  TForm1 = class(TForm)
  ...
  private
    { Private-Deklarationen }
    IniFile: TIniFile;
  ...
  end;

  ...

{------------------------------------------------------------------------------}

procedure TForm1.FormDestroy(Sender: TObject);
begin

  if Assigned(IniFile) then
    IniFile.Free;

end;

{------------------------------------------------------------------------------}

procedure TForm1.ComboBox1Change(Sender: TObject);
var
  Sections: TStringList;
  I: Integer;
begin

  Sections := TStringList.Create;
  try
    with Sections, IniFile, ComboBox1 do
    begin
      ReadSections(Sections);
      for I := 0 to Sections.Count - 1 do
        if ReadString(Sections[I], 'Title', '') = Items[ItemIndex] then
        begin
          txtTitle.Caption := ReadString(Sections[I], 'Title', '');
          txtColour.Caption := ReadString(Sections[I], 'Colour', '');
          txtEngine.Caption := ReadString(Sections[I], 'Engine', '');
          txtTurbo.Caption := ReadString(Sections[I], 'Turbo', '');
        end;
    end;
  finally
    Sections.Free;
  end;

end;

{------------------------------------------------------------------------------}

procedure TForm1.Button1Click(Sender: TObject);
var
  Sections: TStringList;
  I: Integer;
begin

  if OpenDialog1.Execute then
  begin
    if Assigned(IniFile) then
      FreeAndNil(IniFile);
    IniFile := TIniFile.Create(OpenDialog1.FileName);
    Sections := TStringList.Create;
    try
      with IniFile, ComboBox1 do
      begin
        Clear;
        ReadSections(Sections);
        for I := 0 to Sections.Count - 1 do
          if ValueExists(Sections[I], 'Title') then
            Items.Add(ReadString(Sections[I], 'Title', ''));
      end;
    finally
      Sections.Free;
    end;
  end;

end;

{------------------------------------------------------------------------------}

  ...


Alle Zeitangaben in WEZ +1. Es ist jetzt 17:00 Uhr.
Seite 1 von 2  1 2      

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz