【问题标题】:Get a list of database names in combobox items property获取组合框项目属性中的数据库名称列表
【发布时间】:2016-11-08 07:05:03
【问题描述】:

如何在运行时使用位于我的 applicaton.exe 目录中的数据库名称 (*.mdb) 填充组合框项目属性?

【问题讨论】:

  • 请参阅Delphi7, create combobox items 如何在运行时使用项目填充组合框。
  • 查看list all files from a directory in a string grid with delphi 如何使用目录中的文件填充TStrings 列表。
  • 我有这样的想法:procedure TForm1.FormCreate(Sender: TObject); var 数据库:TStringList;我:整数;路径:字符串;开始路径:=ExtractFilePath(Application.ExeName) + '*.mdb';数据库:= TStringList.Create;尝试 i:=0 to databases.Count-1 do combobox1.Items.Add(databases[i]);最后是databases.free;结尾;结束;

标签: delphi


【解决方案1】:

使用TDirectory.GetFiles 函数枚举所有*.mdb 文件并将结果存储在TStringDynArray 中。循环遍历结果并将值插入组合框中。确保包含 System.IOUtilsSystem.Types 单元。

var
  I: Integer;
  MyList: TStringDynArray;
begin
  MyList := TDirectory.GetFiles(ExtractFilePath(ParamStr(0)), '*.mdb',
    TSearchOption.soAllDirectories);
  for I := 0 to Length(MyList) - 1 do
  begin
    ComboBox1.Items.Add(MyList[I]);
  end;
end;

要仅插入文件名而不插入路径,请使用:

ComboBox1.Items.Add(ExtractFileName(MyList[I]));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-08-21
    • 1970-01-01
    • 2019-08-11
    • 1970-01-01
    • 1970-01-01
    • 2023-04-08
    • 1970-01-01
    • 2011-08-10
    相关资源
    最近更新 更多