#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2004
    Posts
    3
    Rep Power
    0

    ComboBoxes and Filenames


    Hey all

    im kinda new to delphi 7, well delphi in general

    my problem is i need to read a filename into a combobox, the file is always a text file and should be in the same directory always. if anyone knows how to do this it would be greatly appreciated. do i need to go from a string variable containing the file location or what im so confused with this. Its prolly very easy

    any ideas are appreciated

    thank Brend0
  2. #2
  3. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,638
    Rep Power
    4247
    I'm not certain what you mean exactly, but doesn't this work?
    Code:
    Combobox1.Text := 'Somefilename';
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2004
    Posts
    3
    Rep Power
    0
    Ok sorry not clear enough. I need to read several "names" of textfiles, but what is in the folder where they are located will change so i cant just write textfile1 and textfile2 i need it so if a person creates textfile3 it will be in the combobox list as well.

    something like

    for allfiles in C:MyProgram/TextFiles/
    put all file 'names' into combobox1.list


    thanks alot
    Last edited by Brend0; April 17th, 2004 at 05:34 AM.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Oct 2000
    Location
    Back in the real world.
    Posts
    5,966
    Rep Power
    190
    You want to search a directory for file names?

    The code looks similar to this:
    Code:
    var
      mySearchRec: TSearchRec;
    begin
      myComboBox.Items.Clear;
      FindFirst('C:\folder\*.*', mySearchRec);
      repeat
        if (mySearchRec.Attributes AND faDirectory)=0 then begin { entry is not a directory }
          myComboBox.Items.Add(mySearchRec.FileName);
        end;
      until FindNext(mySearchRec)=0
    end;
    I don't have Delphi at home (yet), so this is straight from my head. I probably messed up the parameter order as well as the part with the return code of "FindNext()". And I am not sure if it is "myCheckBox.Items" either. So please make sure you check the help file before telling us that it doesn't work.

    Also, the code is faulty in that it doesn't check the success of the FindFirst call and also would not sort out volume labels on floppies.
    But it's weekend now, so this is not my task to solve

    hth,
    M.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2004
    Posts
    3
    Rep Power
    0

    Thumbs up


    Thanks

    thats exactly what i needed to do
    its working perfect now

    thanks M. Hirsch

    from Brend0
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2012
    Posts
    29
    Rep Power
    0
    I tried the code. I had error:
    incompatible types: integer and tsearchrec...

    help please......



    Originally Posted by M.Hirsch
    You want to search a directory for file names?

    The code looks similar to this:
    Code:
    var
      mySearchRec: TSearchRec;
    begin
      myComboBox.Items.Clear;
      FindFirst('C:\folder\*.*', mySearchRec);
      repeat
        if (mySearchRec.Attributes AND faDirectory)=0 then begin { entry is not a directory }
          myComboBox.Items.Add(mySearchRec.FileName);
        end;
      until FindNext(mySearchRec)=0
    end;
    I don't have Delphi at home (yet), so this is straight from my head. I probably messed up the parameter order as well as the part with the return code of "FindNext()". And I am not sure if it is "myCheckBox.Items" either. So please make sure you check the help file before telling us that it doesn't work.

    Also, the code is faulty in that it doesn't check the success of the FindFirst call and also would not sort out volume labels on floppies.
    But it's weekend now, so this is not my task to solve

    hth,
    M.
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2008
    Posts
    354
    Rep Power
    7
    try this:
    Code:
    var
       SearchRec: TSearchRec;
    begin
       if (FindFirst('C:\YourPath\*.txt', faAnyFile-faDirectory, SearchRec) = 0) then
       try
          repeat
             MyComboBox.Items.Add(mySearchRec.FileName);
          until (FindNext(SearchRec <> 0));
       finally
          FindClose(SearchRec);//without close you will have memory leak.
       end;
    end;
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2012
    Posts
    29
    Rep Power
    0
    Modifying a couple lines become:

    procedure TForm2.FormCreate(Sender: TObject);
    begin

    if (findfirst('d:\mevcut\*.txt',faAnyfile-faDirectory,SearchRec)=0) then
    try

    repeat

    combobox1.Items.Add(searchrec.Name);
    until(findnext(searchrec)<>0);
    finally

    findclose(searchrec);
    end;


    end;

    which runs perfectly...
    thanks.




    Originally Posted by majlumbo
    try this:
    Code:
    var
       SearchRec: TSearchRec;
    begin
       if (FindFirst('C:\YourPath\*.txt', faAnyFile-faDirectory, SearchRec) = 0) then
       try
          repeat
             MyComboBox.Items.Add(mySearchRec.FileName);
          until (FindNext(SearchRec <> 0));
       finally
          FindClose(SearchRec);//without close you will have memory leak.
       end;
    end;

IMN logo majestic logo threadwatch logo seochat tools logo