gpt4 book ai didi

Delphi:查找对话框和字符串网格

转载 作者:行者123 更新时间:2023-12-01 18:35:12 25 4
gpt4 key购买 nike

有没有办法使用查找对话框在字符串网格中进行文本搜索?我需要找到一个文本并突出显示它的背景,就像通常找到文本时一样。

谢谢!

最佳答案

像这样:

procedure TForm1.FormClick(Sender: TObject);
begin
FindDialog1.Execute(Handle)
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
FindDialog1.Options := [frDown, frHideWholeWord, frHideUpDown];
end;

procedure TForm1.FindDialog1Find(Sender: TObject);
var
CurX, CurY, GridWidth, GridHeight: integer;
X, Y: integer;
TargetText: string;
CellText: string;
i: integer;
GridRect: TGridRect;
begin
CurX := StringGrid1.Selection.Left + 1;
CurY := StringGrid1.Selection.Top;
GridWidth := StringGrid1.ColCount;
GridHeight := StringGrid1.RowCount;
Y := CurY;
X := CurX;
if frMatchCase in FindDialog1.Options then
TargetText := FindDialog1.FindText
else
TargetText := AnsiLowerCase(FindDialog1.FindText);
while Y < GridHeight do
begin
while X < GridWidth do
begin
if frMatchCase in FindDialog1.Options then
CellText := StringGrid1.Cells[X, Y]
else
CellText := AnsiLowerCase(StringGrid1.Cells[X, Y]);
i := Pos(TargetText, CellText) ;
if i > 0 then
begin
GridRect.Left := X;
GridRect.Right := X;
GridRect.Top := Y;
GridRect.Bottom := Y;
StringGrid1.Selection := GridRect;
Exit;
end;
inc(X);
end;
inc(Y);
X := StringGrid1.FixedCols;
end;
end;

可以轻松扩展此代码以支持向后搜索(“向上”),并且您可能还想实现“匹配整个单词”功能。

也许您只想选择匹配的文本,而不是整个单元格?然后做

  if i > 0 then
begin
GridRect.Left := X;
GridRect.Right := X;
GridRect.Top := Y;
GridRect.Bottom := Y;
StringGrid1.Selection := GridRect;
GetParentForm(StringGrid1).SetFocus;
StringGrid1.SetFocus;
StringGrid1.EditorMode := true;
TCustomEdit(StringGrid1.Components[0]).SelStart := i - 1;
TCustomEdit(StringGrid1.Components[0]).SelLength := length(TargetText);
Exit;
end;

相反。但这会窃取查找对话框的焦点,因此用户将无法按 Return 键选择下一个匹配项,这可能会很烦人。

关于Delphi:查找对话框和字符串网格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7008758/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com