FindText (C++)

From RAD Studio Code Examples
Jump to: navigation, search

Description

This example requires a TRichEdit, a TButton, and a TFindDialog. Clicking the button displays a Find Dialog to the right of the edit control. Filling in the "Find what" text and pressing the Find Next button selects the first matching string in the Rich Edit control that follows the previous selection. Notice that the "Match whole word only" and "Match case" work.

Code

void __fastcall TForm1::Button1Click(TObject *Sender)
{
  FindDialog1->Position = Point(RichEdit1->Left + RichEdit1->Width, RichEdit1->Top);
  FindDialog1->Execute();
}

void __fastcall TForm1::FindDialog1Find(TObject *Sender)
{
  int FoundAt, StartPos, ToEnd;
  TSearchTypes mySearchTypes = TSearchTypes();
  // Begin the search after the current selection,
  // if there is one.
  // Otherwise, begin at the start of the text.
  if (FindDialog1->Options.Contains(frMatchCase))
	mySearchTypes << stMatchCase;
  if (FindDialog1->Options.Contains(frWholeWord))
	mySearchTypes << stWholeWord;
  if (RichEdit1->SelLength)
	StartPos = RichEdit1->SelStart + RichEdit1->SelLength;
  else
	StartPos = 0;
  // ToEnd is the length from StartPos
  // to the end of the text in the rich edit control.
  ToEnd = RichEdit1->Text.Length() - StartPos;
  FoundAt = RichEdit1->FindText(FindDialog1->FindText, StartPos, ToEnd, mySearchTypes);
  if (FoundAt != -1)
  {
	RichEdit1->SetFocus();
	RichEdit1->SelStart = FoundAt;
	RichEdit1->SelLength = FindDialog1->FindText.Length();
  }
  else Beep();
}

__fastcall TForm1::TForm1(TComponent* Owner)
  : TForm(Owner)
{
  RichEdit1->PlainText = False;
  RichEdit1->Lines->LoadFromFile("../Overview.RTF");  // Starts in Debug.
  RichEdit1->ScrollBars = ssVertical;
}

Uses

Personal tools
Assistance