RAD Studio VCL Reference
|
Specifies the position of the first selected character in the text.
property SelStart: Integer;
__property int SelStart;
Read SelStart to determine the position of the first selected character, where 0 indicates the first character. If there is no selected text, SelStart indicates the position of the cursor. Set SelStart to remove the current selection and position the cursor just before the indicated character.
To select a particular range of the text, first set SelStart to position the cursor, and then set SelLength to extend the selection.
C++ Examples:
/* This example requires a TRichEdit, a TButton, and a TFindDialog. Clicking the button will display a Find Dialog to the right of the edit control. Filling in the "Find what" text and pressing the Find Next button will select 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. */ 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(); } void __fastcall TForm1::FormCreate(TObject *Sender) { RichEdit1->PlainText = False; RichEdit1->Lines->LoadFromFile("../Overview.RTF"); // starts in Debug RichEdit1->ScrollBars = ssVertical; }
/* The following OnFind event handler searches a memo component for the text specified in the FindText property of a find dialog component. If found, the first occurrence of the text in Memo1 is selected. The code uses the Pos function to compare strings, and stores the number of characters to skip when determining the selection position in the SkipChars variable. Because there is no handling of case, whole word, or search direction in this algorithm, it is assumed that the Options property of FindDialog1 is set to [frHideMatchCase, frHideWholeWord, frHideUpDown]. */ void __fastcall TForm1::FindDialog1Find(TObject *Sender) { for (int I = 0; I < Memo1->Lines->Count; I++) { int PosReturn = Memo1->Lines->Strings[I].Pos(FindDialog1->FindText); if (PosReturn) //found! { int Skipchars = 0; for (int J = 0; J < I; J++) Skipchars += Memo1->Lines->Strings[J].Length(); Skipchars += I*2; // add CR/LF for all skipped lines Skipchars += PosReturn - 1; Memo1->SetFocus(); Memo1->SelStart = Skipchars; Memo1->SelLength = FindDialog1->FindText.Length(); break; } } }
Delphi Examples:
{ This example requires a TRichEdit, a TButton, and a TFindDialog. Clicking the button click will display a Find Dialog to the right of the edit control. Filling in the "Find what" text and pressing the Find Next button will select the first matching string in the Rich Edit control that follows the previous selection. } procedure TForm1.Button1Click(Sender: TObject); begin FindDialog1.Position := Point(RichEdit1.Left + RichEdit1.Width, RichEdit1.Top); FindDialog1.Execute; end; procedure TForm1.FindDialog1Find(Sender: TObject); var FoundAt: LongInt; StartPos, ToEnd: Integer; mySearchTypes : TSearchTypes; myFindOptions : TFindOptions; begin mySearchTypes := []; with RichEdit1 do begin if frMatchCase in FindDialog1.Options then mySearchTypes := mySearchTypes + [stMatchCase]; if frWholeWord in FindDialog1.Options then mySearchTypes := mySearchTypes + [stWholeWord]; { begin the search after the current selection if there is one } { otherwise, begin at the start of the text } if SelLength <> 0 then StartPos := SelStart + SelLength else StartPos := 0; { ToEnd is the length from StartPos to the end of the text in the rich edit control } ToEnd := Length(Text) - StartPos; FoundAt := FindText(FindDialog1.FindText, StartPos, ToEnd, mySearchTypes); if FoundAt <> -1 then begin SetFocus; SelStart := FoundAt; SelLength := Length(FindDialog1.FindText); end else Beep; end; end; procedure TForm1.FormCreate(Sender: TObject); const Path = 'OverView.RTF'; begin RichEdit1.PlainText := False; RichEdit1.Lines.LoadFromFile(Path); RichEdit1.ScrollBars := ssVertical; end;
{ The following OnFind event handler searches a memo component for the text specified in the FindText property of a find dialog component. If found, the first occurrence of the text in Memo1 is selected. The code uses the Pos function to compare strings, and stores the number of characters to skip when determining the selection position in the SkipChars variable. Because there is no handling of case, whole word, or search direction in this algorithm, it is assumed that the Options property of FindDialog1 is set to [frHideMatchCase, frHideWholeWord, frHideUpDown]. } procedure TForm1.FindDialog1Find(Sender: TObject); var I, J, PosReturn, SkipChars: Integer; begin for I := 0 to Memo1.Lines.Count do begin PosReturn := Pos(FindDialog1.FindText,Memo1.Lines[I]); if PosReturn <> 0 then {found!} begin SkipChars := 0; for J := 0 to I - 1 do SkipChars := SkipChars + Length(Memo1.Lines[J]); SkipChars := SkipChars + (I*2); SkipChars := SkipChars + PosReturn - 1; Memo1.SetFocus; Memo1.SelStart := SkipChars; Memo1.SelLength := Length(FindDialog1.FindText); Break; end; end; end; procedure TForm1.FormCreate(Sender: TObject); const Path = 'country.list'; begin Memo1.Lines.LoadFromFile(Path); end;
Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
|
What do you think about this topic? Send feedback!
|