开发者

ListBox Filtering

开发者 https://www.devze.com 2023-01-04 08:43 出处:网络
I Have A ListBox With 10,000 Items And Many Many Duplicate Items! I Wonna Save It To A File Without Duplicate Items (One Item Instead All Copies!) And I Use This Way:

I Have A ListBox With 10,000 Items And Many Many Duplicate Items! I Wonna Save It To A File Without Duplicate Items (One Item Instead All Copies!) And I Use This Way:

Function TMain.List_ExistsIn(ListBox_NAme: TListBox; EParameter: String): Integer;
Var
  i: Integer;
Begin
  EParameter := LowerCase(EParameter);
  Result := -1;
  For i:=0 To ListBox_Name.Items.Count - 1 Do
    If EParameter = Lowercase(ListBox_Name.Items[i]) Then Begin
      Result := i;
      Break;
    End;
End;

I Use Code Above To Detect A Existing Item And Following Procedure To Save It:

Procedure TMain.MakeList(ListBox_Name: TListBox; FileName开发者_如何学运维: String); //================
Var
  i: Integer;
  Temp_ListBox: TListBox;
Begin
  Temp_ListBox := TListBox.Create(Main);
  With Temp_ListBox Do Begin
    Parent := Main;
    Clear;
    For i:=0 To ListBox_Name.Count - 1 Do
      If Main.List_ExistsIn(Temp_ListBox, ListBox_Name.Items[i]) = -1 Then
        Items.Add(ListBox_Name.Items[i]);
    Items.SaveToFile(FileName);
    Free;
  End;
End;

But It Takes A Very Very Long Time To Proceed. Is There Any Better And Fast Way? Thanks.


Try this one

procedure TForm1.FormCreate(Sender: TObject);
var
  StrList: TStringList;
  I: Integer;
begin
  StrList := TStringList.Create;
  StrList.Sorted := True;
  StrList.Duplicates := dupIgnore;
  StrList.AddStrings(ListBox1.Items);  //Your List Box Items
  StrList.SaveToFile('C:\abc.txt');
  StrList.Free; //Cleanup
end;


Watch out for the infamous CompareString() effect...

Insert 59A, 5-9A, 59-A, -59-A into sorted list 1. The list becomes 59A, -59-A, 5-9A, 59-A and .Find() or .IndexOf() will fail to locate 59-A.

Now insert the same values into sorted list 2, but insert in the order 59A, -59-A, 5-9A, 59-A. The list becomes 59A, 59-A, -59-A, 5-9A. .Find() and .IndexOf() can locate 59-A.

See this blog for more details.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号