【问题标题】:Read mutiple values from ini file into TCombobox将 ini 文件中的多个值读入 Combobox
【发布时间】:2016-06-06 13:51:41
【问题描述】:

我有一个包含以下内容的ini文件:

[Colours]
1 = Red
2 = Blue
3 = Green
4 = Yellow

在我的应用程序中,我有一个 TComboBox,我想用 ini 文件中的颜色填充它。

有人知道我会怎么做吗?

谢谢,

【问题讨论】:

    标签: delphi delphi-xe8


    【解决方案1】:

    您可以使用TIniFile.ReadSection() 获取一个部分中的名称列表,然后迭代获取值:

    procedure TForm1.LoadFile(const AFilename: String);
    var
      I: TIniFile;
      L: TStringList;
      X: Integer;
      N: String;
      V: String;
    begin
      I:= TIniFile.Create(AFilename);
      try
        L:= TStringList.Create;
        try
          ComboBox1.Items.Clear;
          I.ReadSection('Colours', L);
          for X := 0 to L.Count-1 do begin
            N:= L[X]; //The Name
            V:= I.ReadString('Colours', N, ''); //The Value
            ComboBox1.Items.Add(V);
          end;
        finally
          L.Free;
        end;
      finally
        I.Free;
      end;
    end;
    

    作为替代方案,您还可以将部分中的名称/值对转储到单个 TStringList 并使用字符串列表的内置功能读取每个值...

    procedure TForm1.LoadFile(const AFilename: String);
    var
      I: TIniFile;
      L: TStringList;
      X: Integer;
      N: String;
      V: String;
    begin
      I:= TIniFile.Create(AFilename);
      try
        L:= TStringList.Create;
        try
          ComboBox1.Items.Clear;
          I.ReadSectionValues('Colours', L);
          for X := 0 to L.Count-1 do begin
            N:= L.Names[X]; //The Name
            V:= L.Values[N]; //The Value
            ComboBox1.Items.Add(V);
          end;
        finally
          L.Free;
        end;
      finally
        I.Free;
      end;
    end;
    

    附带说明一下,Ini 文件的 = 符号两边都没有空格,除非您当然希望该空格作为实际名称或值的一部分。

    【讨论】:

    • ComboBox1.ItemsTStrings,但 L 也是 TStrings。您可以直接将L 分配给ComboBox1.Items,无需循环。
    • @Rudy 循环不仅用于添加项目,还用于提取值。起初我得到了分配列表的答案,直到我意识到列表实际上包含名称,而不是值。
    • @JerryDodge: ReadSection() 仅提取名称,因此您必须单独调用 ReadString() 才能读取值。如果您改用ReadSectionValues(),那么它会同时返回名称和值,因此您根本不必调用ReadString()。然后,您可以根据需要使用TStrings.Names[]TStrings.ValueFromIndex[] 属性循环访问TStrings
    • @RemyLebeau 没错,但ReadSectionValues() 返回 名称和值,例如1 = Red 而不仅仅是Red。所以你仍然需要迭代任何一种方式。我的意思是,您不能像 Rudy 建议的那样将列表分配给 Items 属性。
    • @JerryDodge: ReadSectionValues()name=value 字符串填充TStringsNames[]Value[]ValueFromIndex[] 属性知道如何拆分name=value 字符串。是的,您必须进行迭代,以便可以拆分 INI 数据并根据需要使用各个名称和值。我不会直接从 INI 填充 ComboBox。
    【解决方案2】:

    试试这个,不用两次读取文件:

    uses IniFiles;

    procedure TForm1.Button1Click(Sender: TObject);
    var
      lIni : TIniFile;
      i: Integer;
    begin
      lIni := TIniFile.Create('c:\MyFile.ini');
      try
        lIni.ReadSectionValues('Colours', ComboBox1.Items);
        for i := 0 to ComboBox1.Items.Count - 1 do
          ComboBox1.Items[i] := ComboBox1.Items.ValueFromIndex[i];
      finally
        FreeAndNil(lIni);
      end;
    end;
    

    【讨论】:

      猜你喜欢
      • 2011-12-14
      • 1970-01-01
      • 2014-02-20
      • 2011-12-26
      • 2022-01-21
      • 1970-01-01
      • 2015-09-17
      • 2010-09-18
      相关资源
      最近更新 更多