简体   繁体   English

如何滚动到用户在finddialog框中输入的“找到”单词

[英]How to scroll to the 'found' word that the user has entered in the finddialog box

I have a rich edit on a delphi form that includes a finddialog box. 我在delphi表单上有一个丰富的编辑,其中包含一个finddialog框。 The user enters the word to be found, and the program correctly highlights the 'found' word -this much works fine. 用户输入要找到的单词,程序正确地突出显示“找到的”单词 - 这很好用。 What I want, however, is for the program to scroll so that the first occurrence of the found word appears on the first line in the richedit box. 然而,我想要的是程序滚动,以便找到的单词的第一个出现在richedit框的第一行。 Should the user then click 'next,' (finddialog box)then the scrolling continues so that the next occurrence of the word then appears on the first line, and so on. 如果用户然后单击“下一步”(查找对话框),则滚动继续,以便下一次出现的单词出现在第一行,依此类推。 Can anyone help me out here? 有人可以帮我从这里出去吗?

procedure tform3.FindDialog1Find(Sender: TObject);
var
  sText: string;
  StartFrom, FoundPos: integer;
begin
  { If saved position is 0, this must be a "Find First" operation. }
  if PreviousFoundPos = 0 then
    { Reset the frFindNext flag of the FindDialog }
    findDialog1.Options := findDialog1.Options - [frFindNext];

  if not (frFindNext in FindDialog1.Options) then begin // is it a Find "First" ?
    sText := Richedit1.Text;
    StartFrom := 1;
  end
  else begin // it's a Find "Next"
    { Calculate from where to start searching:
      start AFTER the end of the last found position. }
    StartFrom := PreviousFoundPos + Length(FindDialog1.Findtext);
    { Get text from the RichEdit, starting from StartFrom }
    sText := Copy(Richedit1.Text, StartFrom, Length(Richedit1.Text) - StartFrom + 1);
  end;

  if frMatchCase in FindDialog1.Options then   // case-sensitive search?
    { Search position of FindText in sText.
      Note that function Pos() is case-sensitive. }
    FoundPos := Pos(FindDialog1.FindText, sText)
  else
    { Search position of FindText, converted to uppercase,
      in sText, also converted to uppercase        }
    FoundPos := Pos(UpperCase(FindDialog1.FindText), UpperCase(sText));

  if FoundPos > 0 then begin
    { If found, calculate position of FindText in the RichEdit }
    PreviousFoundPos := FoundPos + StartFrom - 1;
    { Highlight the text that was found }
    RichEdit1.SelStart := PreviousFoundPos - 1;
    RichEdit1.SelLength := Length(FindDialog1.FindText);
    RichEdit1.SetFocus;
  end
  else
    ShowMessage('Could not find "' + FindDialog1.FindText + '"');
end;

You can use the following code to scroll to the cursor position in the TRichEdit control: 您可以使用以下代码滚动到TRichEdit控件中的光标位置:

RichEdit1.SelStart := PreviousFoundPos - 1;
RichEdit1.SelLength := Length(FindDialog1.FindText);
RichEdit1.SetFocus;   

// Now scroll to the current cursor position 
RichEdit1.Perform(EM_SCROLLCARET, 0, 0);

This sends the Windows message EM_SCROLLCARET to the control which will make the control scroll to the current cursor position. 这将Windows消息EM_SCROLLCARET发送到控件,该控件将使控件滚动到当前光标位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何检查用户是否在 TDateTimePicker 中输入了值? - How can I check if the user has entered a value into a TDateTimePicker? 如何让FindDialog保持最佳状态(Delphi)? - How Can I Keep the FindDialog from Staying on Top (Delphi)? FMX firemonkey如何在运行时滚动垂直滚动框 - FMX firemonkey how to Scroll a vertical scroll box in runtime 如何使用TEdit框输入密码而不显示输入的字符,只显示* - How to use a TEdit box to enter password without showing the characters that is bing entered and only showing a * 如何在背景不同的Word文件中查找文本? - How to find text inside word file that has different background? 用于清理用户输入的URL的Delphi代码 - Delphi code for sanitizing a URL entered by the user 如何保存TWebBrowser的内容,包括用户输入的表单值? - How do I save the contents of TWebBrowser, including user-entered form values? 为什么我的光标没有变成Delphi中的FindDialog中的沙漏? - Why doesn't my cursor change to an Hourglass in my FindDialog in Delphi? 如何创建无模式对话框并在用户单击确定时将其关闭? - How to create a modeless dialog box and close it when the user clicks ok? 甚至在将所有值输入编辑框之前,都会启动错误检查 - Error check kicks in even before entire value is entered in an edit box
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM