2010-04-22 46 views
2

我想通過檢查兩個其他列表框來清理列表框。德爾福 - 清潔TListBox物品

  • listBox1中會包含項目的大名單
  • Listbox2將有句話我想從ListBox 1中刪除
  • Listbox3必須有存在強制性的話它留在ListBox1的

下面是我迄今爲止得到的代碼,它對於大型列表非常慢。

// not very efficient 
Function checknegative (instring : String; ListBox : TListBox) : Boolean; 
Var 
    i : Integer; 
Begin 
    For I := listbox.Items.Count - 1 Downto 0 Do 
    Begin 
    If ExistWordInString (instring, listbox.Items.Strings [i], 
     [soWholeWord, soDown]) = True 
    Then 
    Begin 
     result := True; //True if in list, False if not. 
     break; 
    End 
    Else 
    Begin 
     result := False; 
    End; 
    End; 
    result:=false; 
End; 

Function ExistWordInString (aString, aSearchString : String; 
    aSearchOptions : TStringSearchOptions) : Boolean; 
Var 
    Size : Integer; 
Begin 
    Size := Length (aString); 
    If SearchBuf (Pchar (aString), Size, 0, 0, aSearchString, aSearchOptions) <> Nil Then 
    Begin 
    result := True; 
    End 
    Else 
    Begin 
    result := False; 
    End; 
End; 
+0

我不明白你如何在你的代碼中使用這兩個函數。 什麼是你的checknegative函數中的「instring」參數? – LeGEC 2010-04-23 07:29:40

+0

instring是一個字符串。如果單詞「雞」在列表框中,則它返回爲真,否則爲假。 – Brad 2010-05-08 21:32:30

回答

3

如果這是在列表框上工作,那麼每次都可能花費大量時間來重新繪製所有內容。您可以禁用此行爲。環繞最外面的環路與此:

listbox.Items.BeginUpdate; 
try 
    //do the loop here 
finally 
    listbox.Items.EndUpdate; 
end; 

此外,您可以直接分配一個布爾值,布爾表達式的評估,這將節省一些時間在你的內部循環。所以:

Function ExistWordInString (aString, aSearchString : String; aSearchOptions : TStringSearchOptions) : Boolean; 
Var 
    Size : Integer; 
Begin 
    Size := Length (aString); 
    result := SearchBuf (Pchar (aString), Size, 0, 0, aSearchString, aSearchOptions) <> Nil; 
End; 

不知道會有多少差異,但。如果您進行了這些更改並且速度太慢,請嘗試通過諸如Sampling Profiler這樣的分析器來運行您的程序,這將幫助您瞭解您的代碼大部分時間都在花費什麼。

+0

我正在做開始/結束更新,我會挑戰性地看着採樣分析器。 – Brad 2010-04-22 13:48:08

+0

看着你的StringListComp,它可能會給我一些更好的方法來解決我在做什麼... – Brad 2010-04-22 13:52:37

+0

@Brad:你的意思是我的博客上的圖書館?這對於比較整個字符串非常有用。但是,當你可以使用它時,它非常有用。 – 2010-04-22 14:03:54

4

如果您在控件中使用TStrings實例執行任何操作,則創建一個臨時實例TStringList可能會有所幫助,請將控件項分配給它,然後使用臨時列表。

其原因是,在列表框,組合框的Items財產或類似的通過是不成立的字符串本身就是一個代理類實現,但它使用的Windows消息像LB_GETCOUNTLB_GETTEXT檢索元素直接來自本地Windows控件。如果你多次訪問一個字符串列表項,那麼重複的消息處理開銷將加起來

+0

偉大的信息知道! – Brad 2010-04-22 17:48:37