【问题标题】:How to correctly add sections of the .ini file to ComboBox如何正确地将 .ini 文件的部分添加到 ComboBox
【发布时间】:2018-01-13 23:49:24
【问题描述】:

如何正确地将.ini文件的各个部分添加到ComboBox中,根据在ComboBox中选择的键值显示在Label和shell中执行打开选中的网页

我的 .ini 文件

[Google]
Adress=https://www.google.co.uk
Description=Example description1
[Ask]
Adress=http://www.ask.com
Description=Example description2
[Bing]
Adress=https://www.bing.com
Description=Example description3

我的代码:

var
  Form1: TForm1;
  INI: TIniFile;
implementation

procedure TForm1.Button4Click(Sender: TObject);
begin
  INI := TIniFile.Create(ExtractFilePath(Application.ExeName) + 'setup.ini');
  try
    INI.ReadSections(ComboBox1.Items);
  finally
    INI.Free;
  end;
end;

procedure TForm1.ComboBox1Change(Sender: TObject);
  var
  AdressIni:string;
  begin
  AdressIni := INI.ReadString(ComboBox1.Items[ComboBox1.ItemIndex],'Adress', '');
  Label1.Caption := INI.ReadString(ComboBox1.Items[ComboBox1.ItemIndex],'Description', '');
  ShellExecute(handle, 'open', 'AdressIni', nil, nil, sw_shownormal);
  end;
end.

【问题讨论】:

  • 所以您正尝试使用从 ini 中获取的数据填充组合框?
  • 是的。并继续使用键值,就像代码中一样
  • stackoverflow.com/q/9338283/62576 将向您展示如何使用组合框/inifile 部分。

标签: delphi ini


【解决方案1】:

您将错误的参数传递给ShellExecute 过程。您将 AdressIni 作为第一个参数 (URL) 传递的字符串传递,而不是传递对 AdressIni 变量的引用。

你可以这样做:

procedure TForm3.ComboBox1Select(Sender: TObject);
var AdressIni: string;
begin
  Ini := TIniFile.Create('D:\Proba.ini');
  try
    //Form3.Caption := INI.ReadString(ComboBox1.Items[ComboBox1.ItemIndex],'Adress', '');
    AdressIni := INI.ReadString(ComboBox1.Text,'Adress','');
    Form3.Caption := AdressIni;
    ShellExecute(handle, 'open', PCHAR(AdressIni), nil, nil, sw_shownormal);
  finally
    INI.Free;
  end
end;

请注意,我正在阅读 ComboBox1.Text 而不是 ComboBox1.Items[ComboBox1.ItemIndex],因为 ComboBox1.Text 包含当前选定项目的文本。

【讨论】:

  • 你的意思是它不起作用?我在发布之前在我的电脑上测试过,效果很好
  • 这是如何工作的?如果您还没有将 .ini 文件添加到您的 ComboBox?当我添加时,项目窗口将打开,而不是 WWW 页面。而且标签不显示任何东西
  • 您仍然按照过去使用自己的代码的方式填充 ComboBox。还要确保您使用的是正确的 INI 文件路径(我在代码中使用了不同的路径)。因为如果你不是 INI.ReadString 将返回空(默认)字符串
猜你喜欢
  • 2016-01-24
  • 2023-03-08
  • 1970-01-01
  • 2016-12-17
  • 1970-01-01
  • 2015-02-05
  • 2015-01-10
  • 2015-10-24
  • 1970-01-01
相关资源
最近更新 更多