PDA

View Full Version : Adding FindFirst/Next to TListView



Septimus
22-07-2003, 11:07 AM
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?



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...

Eriken
22-07-2003, 02:27 PM
FindFirst and FindNext works like a charm..

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);


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

wilbur989
22-07-2003, 05:24 PM
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...
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;


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:

Septimus
24-07-2003, 06:23 AM
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...