2004. november 6., szombat

Search up and down in a TListView


Problem/Question/Abstract:

How to search up and down in a TListView

Answer:

procedure TRememberMenu.FindDialog1Find(Sender: TObject);
var
  MaxLines, Adder, CurrentLine, X: Integer;
  LookText: string;
  Found: Boolean;
begin
  {Find the word}
  Found := False;
  MaxLines := ListView1.Items.Count - 1;
  LookText := UpperCase(FindDialog1.FindText);
  if ListView1.Selected <> nil then
    CurrentLine := TListItem(ListView1.Selected).Index
  else
    CurrentLine := 0;
  X := CurrentLine;
  if frDown in FindDialog1.Options then
  begin
    Adder := 1;
  end
  else
  begin
    Adder := -1;
  end;
  X := X + Adder;
  if (X >= 0) and (X <= MaxLines) then
  begin
    repeat
      begin
        if Pos(LookText, UpperCase(ListView1.Items[X].Caption)) <> 0 then
        begin
          Found := True;
          ListView1.Selected := nil;
          ListView1.Selected := ListView1.Items[X];
          ListView1.Items[X].MakeVisible(False);
          ListView1.SetFocus;
        end;
        X := X + Adder;
      end;
    until
      (X > MaxLines) or (Found) or (X < 0);
  end;
  if Found = False then
  begin
    SoundBeep;
  end;
end;

Nincsenek megjegyzések:

Megjegyzés küldése