Recent

Author Topic: TEdit's Autoselect  (Read 2562 times)

Weitentaaal

  • Sr. Member
  • ****
  • Posts: 367
  • Weitental is a very beautiful garbage depot.
TEdit's Autoselect
« on: January 25, 2022, 04:33:09 pm »
Hello Guys,

i wanted to do following: When the User clicks in a certain field(TEdit), the whole text in the field should be selected.

i tried:

-> Working with SelLength and SelStart (no effect):

Code: Pascal  [Select][+][-]
  1. procedure TMyClass.TxEnter(Sender: TObject);
  2. begin
  3.    Tx.SelStart:= 0;
  4.    Tx.SelLength:= Length(Tx.Text);
  5. end;
  6.  

-> The Porperty "Autoselect" (Standard set to True), but i had no effect on the Edit either

-> The Function SelectAll (no effect again)

Code: Pascal  [Select][+][-]
  1. procedure TMyClass.TxEnter(Sender: TObject);
  2. begin
  3.    Tx.SelectAll;
  4. end;
  5.  

found out that the "SelectAll" Function and the "SelStart" and "SelLength" Functions only work in the OnClick event handler. But the Problem here is that i wanted to create a shared handler for this function and in some fileds i already have code in the onClick event.

So back to the AutoSelect Property. Got it checked in the Object inspector but with no effect. Could someone help me ou please ? thanks in advance !

Lazarus: 2.0.12 x86_64-win64-win32/win64
Compiler Version: 3.2.0

ASerge

  • Hero Member
  • *****
  • Posts: 1916
Re: TEdit's Autoselect
« Reply #1 on: January 25, 2022, 08:31:02 pm »
i wanted to do following: When the User clicks in a certain field(TEdit), the whole text in the field should be selected.
Example. In new form I appended two Edits, a timer and entered this code:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Edit1Enter(Sender: TObject); // One event for both edits
  2. begin
  3.   Timer1.Enabled := True;
  4. end;
  5.  
  6. procedure TForm1.FormCreate(Sender: TObject);
  7. begin
  8.   Timer1.Interval := 50;
  9.   Timer1.Enabled := False;
  10. end;
  11.  
  12. procedure TForm1.Timer1Timer(Sender: TObject);
  13. begin
  14.   Timer1.Enabled := False;
  15.   if ActiveControl is TEdit then
  16.     TEdit(ActiveControl).SelectAll;
  17. end;

Weitentaaal

  • Sr. Member
  • ****
  • Posts: 367
  • Weitental is a very beautiful garbage depot.
Re: TEdit's Autoselect
« Reply #2 on: January 26, 2022, 08:05:43 am »
looks good ! thanks !

is this only possible with Timers ?
Lazarus: 2.0.12 x86_64-win64-win32/win64
Compiler Version: 3.2.0

Zvoni

  • Hero Member
  • *****
  • Posts: 1179
Re: TEdit's Autoselect
« Reply #3 on: January 26, 2022, 08:35:17 am »
Maybe something you missed for the AutoSelect-Property of TEdit:
Quote
If True, the edit control will select all its text when it receives focus or when the Enter key is pressed
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

loaded

  • Hero Member
  • *****
  • Posts: 568
Re: TEdit's Autoselect
« Reply #4 on: January 26, 2022, 08:51:25 am »
For me it works just fine.

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Edit1Click(Sender: TObject);
  2. begin
  3.   TEdit(sender).SelectAll;
  4. end;  
If Ide=Lazarus 2.0.10 32 Bit and Os=Win 10 Home 64 Bit then Get up and do something useful! Because God is the helper of those who start again;

Zvoni

  • Hero Member
  • *****
  • Posts: 1179
Re: TEdit's Autoselect
« Reply #5 on: January 26, 2022, 09:03:28 am »
For me it works just fine.

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Edit1Click(Sender: TObject);
  2. begin
  3.   TEdit(sender).SelectAll;
  4. end;  
That's probably the OP's "mistake": He's using his variable/TEdit-Name in the Event instead of using the Sender-Object
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

rvk

  • Hero Member
  • *****
  • Posts: 4625
Re: TEdit's Autoselect
« Reply #6 on: January 26, 2022, 10:14:41 am »
For me it works just fine.

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Edit1Click(Sender: TObject);
  2. begin
  3.   TEdit(sender).SelectAll;
  4. end;  
Yeah, but that takes away the possibility to select a place in the edit afterwards.
OP wants the text to be selected ONLY on entering the edit with a mouse click.

Using SelectAll in the OnEnter doesn't work either (and is already done by DoEnter with AutoSelect = true, see the source), but the mouse click is handled afterwards, which loses the selection.

That's why the timer-method works correctly.
(It needs to be done after handling of the mouseclick)

Zvoni

  • Hero Member
  • *****
  • Posts: 1179
Re: TEdit's Autoselect
« Reply #7 on: January 26, 2022, 10:54:28 am »
Yeah, but that takes away the possibility to select a place in the edit afterwards.
OP wants the text to be selected ONLY on entering the edit with a mouse click.

Using SelectAll in the OnEnter doesn't work either (and is already done by DoEnter with AutoSelect = true, see the source), but the mouse click is handled afterwards, which loses the selection.

That's why the timer-method works correctly.
(It needs to be done after handling of the mouseclick)

Hmm, what about the MouseUp-Event (incl. maybe a Flag-Variable. Set Flag to True in Enter-Event, in MouseUp: If Flag Then SelectAll and set Flag=False)?
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

rvk

  • Hero Member
  • *****
  • Posts: 4625
Re: TEdit's Autoselect
« Reply #8 on: January 26, 2022, 10:57:38 am »
Hmm, what about the MouseUp-Event (incl. maybe a Flag-Variable. Set Flag to True in Enter-Event, in MouseUp: If Flag Then SelectAll and set Flag=False)?
Yes, the doing it in the mouseup event with a one time enter-flag might be better, although it is a few lines more code. But with the timer-solution you never know if 50ms is enough (or too much).

Zvoni

  • Hero Member
  • *****
  • Posts: 1179
Re: TEdit's Autoselect
« Reply #9 on: January 26, 2022, 11:00:09 am »
Yep. Works. I added a second TEdit to have something to click around.
Clicking again into Edit1 loses the selection
Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     Edit1: TEdit;
  16.     Edit2: TEdit;
  17.     procedure Edit1Enter(Sender: TObject);
  18.     procedure Edit1MouseUp(Sender: TObject; Button: TMouseButton;
  19.       Shift: TShiftState; X, Y: Integer);
  20.   private
  21.     Flag:Boolean;
  22.   public
  23.  
  24.   end;
  25.  
  26. var
  27.   Form1: TForm1;
  28.  
  29. implementation
  30.  
  31. {$R *.lfm}
  32.  
  33. { TForm1 }
  34.  
  35. procedure TForm1.Edit1Enter(Sender: TObject);
  36. begin
  37.   Flag:=True;
  38. end;
  39.  
  40. procedure TForm1.Edit1MouseUp(Sender: TObject; Button: TMouseButton;
  41.   Shift: TShiftState; X, Y: Integer);
  42. begin
  43.   If Flag Then
  44.     Begin
  45.       Flag:=False;
  46.       TEdit(Sender).SelectAll;
  47.     end;
  48. end;
  49.  
  50. end.
  51.  
  52.  

EDIT: Just tested with multiple TEdit's on the form and with shared Event-procedures (one each for Enter and MouseUp).
Works!
« Last Edit: January 26, 2022, 11:08:19 am by Zvoni »
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

Weitentaaal

  • Sr. Member
  • ****
  • Posts: 367
  • Weitental is a very beautiful garbage depot.
Re: TEdit's Autoselect
« Reply #10 on: January 27, 2022, 08:55:03 am »
Thank You guys for your help, works perfectly now :)
Lazarus: 2.0.12 x86_64-win64-win32/win64
Compiler Version: 3.2.0

 

TinyPortal © 2005-2018