4

How to select all text of a TEdit1 whenever user click on it or click to select some text of it

menjaraz
  • 7,551
  • 4
  • 41
  • 81
Rafik Bari
  • 4,867
  • 18
  • 73
  • 123
  • I'm confused. Please clarify your question. Do you want to know how to set TEdit to the appropriate behavior or how to do to do it programmatically? – menjaraz Dec 23 '11 at 13:32

4 Answers4

22

It can be quite dangerous to do anything beyond the default behaviour of the TEdit control. Your users know how the standard Windows controls behave and any deviation from this is likely to cause confusion.

By default the AutoSelect property is set to True.

Determines whether all the text in the edit control is automatically selected when the control gets focus.

Set AutoSelect to select all the text when the edit control gets focus. AutoSelect only applies to single-line edit controls.

Use AutoSelect when the user is more likely to replace the text in the edit control than to append to it.

When this property is True, the entire contents of the edit control are selected when it gets the focus by means of keyboard action. If the control gets the focus by a mouse click then the contents will not all be selected. In that case you simply press CTRL+A to select all. A double click will select the word underneath the mouse. This is all standard behaviour implemented by the underlying Windows control.


If you change the select in response to the OnClick event, as per the currently selected answer, then you will find that it is impossible to move the caret with a mouse click. This is exceedingly counter-intuitive behaviour.

This is a classic example of why you need to be very careful about changing the behaviour of a control from its default. It's simply very easy not to miss a particular use case when testing but when your users get hold of the program, they are sure to find all such wrinkles.

What you could safely do is to call SelectAll from OnDblClick. This would, I believe have no annoying side-effects.

Another option would be to call SelectAll when the focus switched to the edit control, but not every time you click in the control. This might feel a little odd to the user, but I personally think it would be reasonable to take this course of action. If you want to do this you need to handle the OnEnter event of your edit control:

procedure TForm1.Edit1Enter(Sender: TObject);
begin
  PostMessage(Edit1.Handle, EM_SETSEL, 0, -1);
end;
Community
  • 1
  • 1
David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
12

How to select all text of a TEdit1 whenever user click on it

Select Edit1 in the VCL editor and double-click on the OnClick event:

enter image description here

procedure TForm13.Edit1Click(Sender: TObject);
begin
  Edit1.SelectAll;
end;

You can also link this event to another control like a button.
Select the button, choose and click on the V arrow to select an event you want to link.

enter image description here

Now both Edit1.OnClick and Button1.OnClick link to the same event.

Johan
  • 74,508
  • 24
  • 191
  • 319
  • This is just an event handler. It must must be wired somewhere to be triggered. – menjaraz Dec 23 '11 at 13:25
  • I mean wired to an event property of a VCL component. Presumably Edit1 judging by its name but it might also not be so. – menjaraz Dec 23 '11 at 13:34
  • @Johan Have you tried using such an edit control. Try placing the caret with your mouse. – David Heffernan Dec 23 '11 at 14:03
  • @DavidHeffernan, I wanted to keep it simple, but put an extra test in to only do `selectall` if the edit does not have the focus. Anyway good point, the simple change makes it much less annoying. – Johan Dec 23 '11 at 15:52
  • 1
    It's too late to put the test in the OnClick. The best you can do is to post a select all message to the window in `OnEnter`. – David Heffernan Dec 23 '11 at 16:00
  • Reverted the edit i don't want to overcomplicate this with postmessage. I just wanted to show in general how to link code to events. – Johan Dec 23 '11 at 21:33
3

How to select some text of a TEdit1 whenever user click on it:

procedure TForm1.Edit1Click(Sender: TObject);
begin
  Edit1.SelStart:= 1;
  Edit1.SelLength:= 2;
end;
kludg
  • 27,213
  • 5
  • 67
  • 118
1

You must use OnMouseUp;

procedure cxMRUEdit1MouseUp(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
 if Button=mbLeft then cxMRUEdit1.SelectAll;
end;