Question

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

Was it helpful?

Solution

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;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top