2010-04-22 26 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