Roland Bengtsson
Roland Bengtsson

Reputation: 5158

How can I use TEdits selStart and SelLength to select Text?

I try to make a new project add a TEdit and a TButton. Set Edit1.Text to 'This is a test message'. And add an event to the button:

procedure TForm7.Button1Click(Sender: TObject);
begin
  Edit1.SelStart := 5;
  Edit1.SelLength := 5;
end;

Nothing is selected when I click the button. Can someone explain why and how it should be done to select some part of the text ?

Regards Roland

Upvotes: 2

Views: 4494

Answers (1)

TLama
TLama

Reputation: 76693

It works as expected, but since your button stealed the focus by clicking on it, you're then trying to focus that edit box back again. And by focusing an edit box, all of its text is selected by default. Here's a simple proof, that the text is selected if the edit box has focus while selecting:

procedure TForm1.Button1Click(Sender: TObject);
begin
  Edit1.SetFocus;
  Edit1.SelStart := 5;
  Edit1.SelLength := 5;
end;

Upvotes: 9

Related Questions