Results 1 to 4 of 4

Thread: Adding FindFirst/Next to TListView

  1. #1

    Adding FindFirst/Next to TListView

    I think that about an hour is quite enough time to stuff around with a simple problem, so I thought I'd quit before I got too frustrated and post on here.

    I'm trying to make a small program to search my mp3 collection for a song and then allow me to either immediately play or enqueue the results in winamp.

    The problem here is that the name and size of the files are added on different lines. In fact, the sizes of all but one file are added first (in the size column) and then the names of the files are added underneath (in the name column). And finally the last file size is added to the end of them (in the size column).

    Maybe I'm completely misinterpreting the FindFirst/Next functions, but this should work properly shouldn't it?

    Code:
    procedure TForm1.SearchButClick(Sender: TObject);
    var sr: TSearchRec;
    begin
      with Output.Items do begin
        Clear;
        if FindFirst('E:\Music\*' + SearchBox.Text + '*.mp3', faAnyFile, sr) = 0 then begin
          repeat
            Add.Caption := sr.Name;
            Add.SubItems.Add(IntToStr(sr.Size));
          until FindNext&#40;sr&#41; <> 0;
          FindClose&#40;sr&#41;;
        end;
      end;
    Oh, and also how can I make it alternate alphabetical sorting by clicking on the name column? You know, click once and it's A-Z, click again and it's Z-A...
    Why are toasters created with a setting that burns the bread to a crisp no sane person could eat?
    <br />Who said Humpty Dumpty was an egg?
    <br />Why are there no 'B' sized batteries?
    <br />Why don't you ever see baby seagulls?

  2. #2

    Adding FindFirst/Next to TListView

    FindFirst and FindNext works like a charm..
    [pascal]
    if FindFirst('E:\Music\*' + Edit1.Text + '*.mp3', faAnyFile, sr) = 0 then
    Edit2.text := sr.Name;

    if FindNext(sr) = 0 then // If next exist add it to Edit3.text box :-)
    Edit3.text := sr.Name;
    FindClose(sr);
    [/pascal]

    Abit difficult to say why it doesn't work at your end I guess, what happens when you try it and what is Output.Items?
    _____
    Eriken

  3. #3

    TList View

    What you have for the TSearchRec structure is perfect the problem resides in your implimentation of adding subItems to the list. Here is your code block corrected...
    [pascal]procedure TForm1.SearchButClick(Sender: TObject);
    var sr: TSearchRec;
    LI: TListItem;
    begin
    with Output.Items do begin
    Clear;
    if FindFirst('E:\Music\*' + SearchBox.Text + '*.mp3', faAnyFile, sr) = 0 then begin
    repeat
    LI := Add;
    LI.Caption := sr.Name;
    LI.SubItems.Add(IntToStr(sr.Size));
    until FindNext(sr) <> 0;
    FindClose(sr);
    end;
    end;
    [/pascal]

    Because you were calling add twice you were creating a new listitem each time add was called. Because you don't want to empty your list box you do not need to free LI. You should only nil it when you are done. ie. LI := nil; Hope that helps.

    -Jeremy

    p.s. I never professed to be able to spell correctly... :lol:

  4. #4

    Adding FindFirst/Next to TListView

    As with most things in programming I have no idea how that works, but it does. As far as I can see all it does is replace the Add word with the word Add stored in a variable. Like doing 1 + 1 or 1 + X where X := 1 anyway.

    I've done it the way I posted several times before with no problem, so I don't know why it would be any different this time.

    Anyway, thanks a lot for your help. I'll look it over a few more times and see if I can figure it all out.

    I'd love to know how to make it alternate alphabetical sorting by clicking on the name column too. You know, click once and it's A-Z, click again and it's Z-A...
    Why are toasters created with a setting that burns the bread to a crisp no sane person could eat?
    <br />Who said Humpty Dumpty was an egg?
    <br />Why are there no 'B' sized batteries?
    <br />Why don't you ever see baby seagulls?

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •