我有一个搜索字段,用户搜索每个输入的字符。所以没有搜索按钮。问题是搜索速度很慢。我的想法是在每个键输入一点后延迟搜索,所以如果用户快速输入搜索字符串,则不用搜索就可以进行搜索。然后执行搜索。我的代码到目前为止。如何搜索每一个性能更好的字符?
procedure TAgreementModuleForm.SetIsSearching(const Value: Boolean);
begin
fIsSearching := Value;
tmrDelayKey.Enabled := Value;
end;
procedure TAgreementModuleForm.tmrDelayKeyTimer(Sender: TObject);
begin
IsSearching := True;
end;
procedure TAgreementModuleForm.txtSearchAgreementCustomerExit(Sender: TObject);
begin
IsSearching := False;
end;
procedure TAgreementModuleForm.txtSearchAgreementCustomerKeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
var
vSearchExpression: string;
begin
if IsSearching then
begin
vSearchExpression := Trim(txtSearchAgreementCustomer.Text);
IsSearching := False;
if vSearchExpression <> '' then
DoSearch(vSearchExpression);
end
else
tmrDelayKey.Enabled := True;
end;
tmrDelayKey是延时为1000ms的TTimer。 IsSearching是一个布尔属性。
这是用户停止编写搜索时无法使用的代码。 这怎么能改进?
您可能会拦截Char(13)以根据需要进行搜索。您可以重置OnKeyup中的计时器(disbled/enabled),并处理OnTimer内的计时器搜索和禁用。 – bummi
对于那个搜索编辑,我会写一个组件['像这样'](http://pastebin.com/45Z2d7pE)。它考虑到了任何变化,而不仅仅是当用户在键盘上键入时(您应该考虑像通过弹出菜单粘贴文本等情况)。 – TLama