How to select Tedit field text in Delphi 10

Asked

Viewed 1,184 times

3

I have a very basic problem and I can’t figure out the cause... I need the text from within a Tedit to be whole selected when the field receives focus. In the times of Delphi 7 with VCL I was only doing TEdit(Sender).SelLength := Length(TEdit(Sender).Text) at the event OnEnter. Now with Delphi 10.2 and Firemonkey I’ve tried several different ways, but it doesn’t work.

Example:

procedure TfPrincipal.Edit1Enter(Sender: TObject);
begin
   TEdit(Sender).SetFocus;
   TEdit(Sender).SelStart  := 0; // Ja tentei mudar este valor
   TEdit(Sender).SelLength := Length(TEdit(Sender).Text); // Ja tentei mudar este valor também
end;

In Firemonkey it works different?

  • 1

    I took the test and it really doesn’t work. see the answer.

1 answer

1

Another thing set to SelLength a value greater than the number of characters of SelStart results in the selection of all characters from SelStart until the end of the text.

TEdit(Sender).SetFocus ; 
TEdit(Sender).SelStart  : = 0; 
TEdit(Sender).SelLength := Length(TEdit(Sender).Text)+1;
  • 1

    I ran the tests on Delphi 10.1 Berlin and it worked perfectly. Add a +1 in Length size.

  • 1

    something else you can remove the TEdit(Sender).SetFocus; since in the control entrance it is already with the focus.

  • 1

    Eduardo, you did your tests using the event OnClick or OnEnter do Tedit? At the event OnEnter, which is the event I’m using, it doesn’t work. So I’m adapting a little bit more code and using the OnClick. Already the property HideSelection I did not find in Delphi 10.2 Tokyo.

  • 1

    both with the OnClick I put the SetFocus and in the OnEnter without the SetFocus.

  • 1

    I don’t know... very strange what is happening here. It doesn’t work either in Windows or Android with event OnEnter (and there is no Hideselection property). But thanks for your attention

  • 1

    Hideselection I got confused ... is for vlc I will edit the answer

  • Oh yes, for VCL everything works OK.

  • 1

    I just took the test on android with the Button is selects normal in both windows and android more when entering the field only works in windows I will check

  • 1

    I just tested with an Edit1tap event both the Edit1enter they work the problem I realized is that when it enters the Control the focus goes away probably because of the virtual keyboard that comes in front.

  • 1

    This parade is strange... But don’t worry. I will use the event OnClick, adapting some things. I don’t like doing this, but you can’t spend too much energy on certain things as well. Thank you!

  • 1

    mark how answered if helped you...

  • I can only mark as a valid reply if the problem has been solved as proposed, which in this case is in Firemonkey with event OnEnter Tedit. Your tip does not apply to this case and I had to work around the problem using the event OnClick as I commented before. But thank you!

Show 7 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.