Topic: Files into Combobox

Hello,

i export some files in a certain folder. Now, i want to show the filenames in this folder in a combobox. How can i do that?

regards

Re: Files into Combobox

Hello,


procedure Form1_OnShow (Sender: string; Action: string);
var
   i, c: integer;
   sl: TStringList;
begin
     sl := TStringList.Create;
     sl.Text := GetFilesList('c:\Films');

     c := sl.Count - 1;
     for i := 0 to c do Form1.ComboBox1.dbAddRecord(-1, sl[i]);

     sl.Free;
end;

Also I made example for you, please download here:

Post's attachments

Attachment icon List files in combobox.zip 1.9 kb, 673 downloads since 2014-10-24 

Dmitry.

Re: Files into Combobox

It works. Thank you.

Re: Files into Combobox

I Need a part of a string... strreplace doesnt work in my enviroment... what ways are there to get subsstrings or removing parts of a string?

Re: Files into Combobox

You can use these functions:

      Delete(var s: String; from, count: Integer)
      Copy(s: String; from, count: Integer): String
      ReplaceStr(const AText, AFromText, AToText: string): string

Here you can find all available function
http://myvisualdatabase.com/forum/misc.php?action=pun_attachment&item=277&download=0


Please, let me example, what exactly you need to do?

Post's attachments

Attachment icon func.png 70.88 kb, 327 downloads since 2014-10-24 

Dmitry.

Re: Files into Combobox

procedure Form1_OnShow (Sender: string; Action: string);
var
   i, c: integer;
   sl: TStringList;
begin
     sl := TStringList.Create;
     sl.Text := GetFilesList('c:\Films');

     c := sl.Count - 1;
     for i := 0 to c do Form1.ComboBox1.dbAddRecord(-1, sl[i]);

     sl.Free;
end;

I only need the name of the files, not the full filepath.
For example "Test.avi" instead of "c:\Films\Test.avi"

Re: Files into Combobox

you can use function ExtractFileName

procedure Form1_OnShow (Sender: string; Action: string);
var
   i, c: integer;
   sl: TStringList;
begin
     sl := TStringList.Create;
     sl.Text := GetFilesList('c:\Films');

     c := sl.Count - 1;
     for i := 0 to c do Form1.ComboBox1.dbAddRecord(-1, ExtractFileName(sl[i]) );

     sl.Free;
end;
Dmitry.

8 (edited by DanielDTR 2014-10-24 12:10:29)

Re: Files into Combobox

It looks good now. Thanks for your great help.

Edit: Last question: How can i clear the Records in the combobox?

Re: Files into Combobox

Hello Dimitry

I have a little puzzled why the script below does not work, I tried so hard to appeal to your mastery

procedure frmDesign_OnShow (Sender: string; Action: string);
var
   i, c: integer;
   sm: TStringList;
begin
  sm := TStringList.Create;
     sm.Text := GetFilesList('f:\Service\Centralizator\Script\');

     c := sm.Count - 1;
     for i := 0 to c do frmDesign.ComboBox1.dbAddRecord(-1, ExtractFileName(sm[i]) );
      frmDesign.Memo1.Text := sm.Text;
           sm.Free;
end;

procedure frmDesign_ComboBox1_OnChange (Sender: string);
var
sm: TStringList;
sf: TStringList;
sss: string;

begin
  // sss := ('f:\Service\Centralizator\Script\'+'+ frmDesign.ComboBox1.Text +'');
  //  ShowMessage(IntToStr(frmDesign.ComboBox1.dbItemID));

//        ShowMessage('' + frmDesign.ComboBox1.Text + '');
        frmDesign.Label1.Caption :='' + frmDesign.ComboBox1.Text + ''; //works
        frmDesign.Label2.Caption := 'f:\Service\Centralizator\Script\'; //works
  frmDesign.Label3.Caption := ''+frmDesign.ComboBox1.Text+''; //works
   //sf := TStringList.Create;
  sf.LoadFromFile('"'f:\Service\Centralizator\Script\'"+"'IntProtherm.txt'"'); //What is wrong? 
  sf.LoadFromFile('"'f:\Service\Centralizator\Script\'"+"'+ frmDesign.ComboBox1.Text +'"'); //What is wrong? , want to select the file that appears in the combobox
    if sf.Count > 1 then ShowMessage(sf[1]); // your  line nr.2 in sl[1]

  frmDesign.Memo1.Text := sf.Text;
end;

procedure frmDesign_Button1_OnClick (Sender: string; var Cancel: boolean);
var
   sl: TStringList;
begin
  sl := TStringList.Create;
     sl.LoadFromFile('123.txt');
     if sl.Count > 1 then ShowMessage(sl[1]); // your  line nr.2 in sl[1]
     frmDesign.Memo1.Text := sl.Text;
     frmDesign.Caption := 'mision complet';

     sl.Free;

end;

Can you help me with a small example how to use the Delete function (var s: String; from, count: Integer) Copy (s: String; from, count: Integer): String, ReplaceStr (const AText, AFromText, AToText : string): string, to see the methodology of application, thanks in advance.

Re: Files into Combobox

DanielDTR
Please, download latest beta version 1.46, where i added new method for ComboBox:
https://www.dropbox.com/s/30ysr48sd8ca4 … 6.zip?dl=0

Form1.ComboBox1.Clear;
Dmitry.

Re: Files into Combobox

einsteinf5
Please, send me your project to support@drive-software.com with link to your post
i'll try to help you


Example how to use Copy, Delete, ReplaceStr:

procedure Form1_Button1_OnClick (Sender: string; var Cancel: boolean);
var
   s: string;
begin
     s := 'One Two';
     s := Copy(s, 5, 3);   //  Copy(s: String; from, count: Integer): String
     ShowMessage(s); // result is "Two"

     s := 'One Two';
     Delete(s, 5, 3);        // Delete(var s: String; from, count: Integer)
     ShowMessage(s); // result is "One"

     s := 'One Two';
     s := ReplaceStr(s, 'Two', 'One');     // ReplaceStr(const AText, AFromText, AToText: string): string
     ShowMessage(s); // result is "One One"
end;
Dmitry.

Re: Files into Combobox

when i use this command Form1.ComboBox1.Clear; in one of my combobox and execute an exit on the form. the next time i open the same form list in the combo box does not appear.

total application close needs to be done before it appears again.

whats the command to show the list?

Re: Files into Combobox

dudes2thed0926carla
You need to clean a component or set to null?
For null try this:

Form1.ComboBox1.dbItemID := -1;
Dmitry.