How can I populate comboboxes items property with database names (*.mdb) residing in my applicaton.exe directory at runtime ?
Asked
Active
Viewed 555 times
-2
-
See [Delphi7, create combobox items](http://stackoverflow.com/q/16496352/576719) how to populate a combobox with items at runtime. – LU RD Nov 08 '16 at 07:11
-
See [list all files from a directory in a string grid with delphi](http://stackoverflow.com/q/11489680/576719) how to fill a `TStrings` list with files from a directory. – LU RD Nov 08 '16 at 07:14
-
I had something like this in mind :procedure TForm1.FormCreate(Sender: TObject); var databases:TStringList; i:integer; path:string; begin path:=ExtractFilePath(Application.ExeName) + '*.mdb'; databases:=TStringList.Create; try for i:=0 to databases.Count-1 do combobox1.Items.Add(databases[i]); finally databases.free; end; end; – user3351050 Nov 08 '16 at 07:45
1 Answers
1
Use the TDirectory.GetFiles function to enumerate all *.mdb files and store the result inside a TStringDynArray. Loop through the results and insert the values into a combobox. Make sure you include the System.IOUtils and System.Types units.
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;
To insert only the file names without the path use:
ComboBox1.Items.Add(ExtractFileName(MyList[I]));