修正 ListView 搜寻问题

问题:如果 SearchEdit 里已输入过搜寻字,再新建 ListView 项目后,会无法显示。

适用:Delphi XE5

源码下载:[原创]修正ListView搜寻问题.zip

procedure TForm1.Button1Click(Sender: TObject);
var i: Integer;
    Item1: TListViewItem;
begin
     ListView1.BeginUpdate;
     ListView1.ClearItems;

     for i:=0 to 30 do
     begin
          Item1 := ListView1.Items.Add;
          Item1.Text := i.ToString;
     end;

     ListView1.EndUpdate;

     // 如果 SearchEdit 里已输入过搜寻字, 再新建项目后, 会无法显示, 需要加入下行才能修正.
     ListView1.ClearSearchEdit;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
     ListView1.SearchEdit.Text := '3';
end;

FMX.ListViewHelper.pas

unit FMX.ListViewHelper;

interface

uses
  FMX.ListView,
  FMX.Edit;

type
  TListViewHelper = class helper for TListView
    function SearchEdit: TSearchBox;
    procedure ClearSearchEdit;
  end;

implementation

{ TListViewHelper }

function TListViewHelper.SearchEdit: TSearchBox;
var i: Integer;
begin
     Result := nil;

     for i:=0 to Self.ComponentCount - 1 do
         if (Self.Components[i] is TSearchBox) then
         begin
              Result := Self.Components[i] as TSearchBox;
              Break;
         end;
end;

procedure TListViewHelper.ClearSearchEdit;
begin
     if SearchEdit <> nil then
     begin
          SearchEdit.Text := ' ';
          SearchEdit.Text := '';
     end;
end;

end.

 

posted @ 2014-03-27 11:26  龟山Aone  阅读(861)  评论(0编辑  收藏  举报