Selecting Text

From RAD Studio
Jump to: navigation, search

Go Up to Working with Text in Controls

For text in an edit control, before you can send any text to the clipboard, that text must be selected. Highlighting of selected text is built into the edit components. When the user selects text, it appears highlighted.

The table below lists properties commonly used to handle selected text.

Property Description
SelText Contains a string representing the selected text in the component.
SelLength Contains the length of a selected string.
SelStart Contains the starting position of a string relative to the beginning of an edit control's text buffer.

For example, 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.

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;

See Also