我想通过检查另外两个列表框来清除列表框。
下面是我到目前为止得到的代码,对于大列表,它非常慢。
// 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;发布于 2010-04-22 17:21:28
如果您在控件中执行任何与TStrings实例循环的操作,创建一个临时TStringList实例可能是有益的,请将控件项分配给它,然后使用临时列表。
原因是列表框、组合框或类似的Items属性是通过一个代理类实现的,该代理类本身不包含字符串,但它使用LB_GETCOUNT和LB_GETTEXT之类的Windows消息直接从本机Windows控件检索元素。如果您多次访问字符串列表项,则重复消息处理的开销将增加。
发布于 2010-04-22 12:52:54
如果这是对一个列表框的工作,它可能需要很多时间来重新绘制每一次。您可以禁用此行为。用以下内容包围最外层的循环:
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;不过,我不知道这会产生多大的影响。如果您做了这些更改,但它仍然太慢,请尝试通过剖析器(如取样剖面仪 )运行您的程序,这将帮助您了解您的代码大部分时间都在做什么。
https://stackoverflow.com/questions/2690698
复制相似问题