TEdit's Autoselect lazarus / 31-LCL ---------------------------------------------------------------------- Weitentaaal · 2022-01-25 15:33 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TMyClass.TxEnter(Sender: TObject);begin   Tx.SelStart:= 0;   Tx.SelLength:= Length(Tx.Text);end;  -> 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TMyClass.TxEnter(Sender: TObject);begin   Tx.SelectAll;end;  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 ! ---------------------------------------------------------------------- ASerge · 2022-01-25 19:31 Quote from: Weitentaaal on January 25, 2022, 04:33:09 pmi 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.Edit1Enter(Sender: TObject); // One event for both editsbegin  Timer1.Enabled := True;end; procedure TForm1.FormCreate(Sender: TObject);begin  Timer1.Interval := 50;  Timer1.Enabled := False;end; procedure TForm1.Timer1Timer(Sender: TObject);begin  Timer1.Enabled := False;  if ActiveControl is TEdit then    TEdit(ActiveControl).SelectAll;end; ---------------------------------------------------------------------- Weitentaaal · 2022-01-26 07:05 looks good ! thanks ! is this only possible with Timers ? ---------------------------------------------------------------------- Zvoni · 2022-01-26 07:35 Maybe something you missed for the AutoSelect-Property of TEdit: QuoteIf True, the edit control will select all its text when it receives focus or when the Enter key is pressed ---------------------------------------------------------------------- loaded · 2022-01-26 07:51 For me it works just fine. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.Edit1Click(Sender: TObject);begin  TEdit(sender).SelectAll;end; ---------------------------------------------------------------------- Zvoni · 2022-01-26 08:03 Quote from: loaded on January 26, 2022, 08:51:25 amFor me it works just fine. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.Edit1Click(Sender: TObject);begin  TEdit(sender).SelectAll;end;  That's probably the OP's "mistake": He's using his variable/TEdit- Name in the Event instead of using the Sender-Object ---------------------------------------------------------------------- rvk · 2022-01-26 09:14 Quote from: loaded on January 26, 2022, 08:51:25 amFor me it works just fine. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.Edit1Click(Sender: TObject);begin  TEdit(sender).SelectAll;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 · 2022-01-26 09:54 Quote from: rvk on January 26, 2022, 10:14:41 amYeah, 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)? ---------------------------------------------------------------------- rvk · 2022-01-26 09:57 Quote from: Zvoni on January 26, 2022, 10:54:28 amHmm, 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 · 2022-01-26 10:00 Yep. Works. I added a second TEdit to have something to click around. Clicking again into Edit1 loses the selection Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i unit Unit1; {$mode objfpc}{$H+} interface uses  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls; type   { TForm1 }   TForm1 = class(TForm)    Edit1: TEdit;    Edit2: TEdit;    procedure Edit1Enter(Sender: TObject);    procedure Edit1MouseUp(Sender: TObject; Button: TMouseButton;      Shift: TShiftState; X, Y: Integer);  private    Flag:Boolean;  public   end; var  Form1: TForm1; implementation {$R *.lfm} { TForm1 } procedure TForm1.Edit1Enter(Sender: TObject);begin  Flag:=True;end; procedure TForm1.Edit1MouseUp(Sender: TObject; Button: TMouseButton;  Shift: TShiftState; X, Y: Integer);begin  If Flag Then    Begin      Flag:=False;      TEdit(Sender).SelectAll;    end;end; end.   EDIT: Just tested with multiple TEdit's on the form and with shared Event-procedures (one each for Enter and MouseUp). Works! ---------------------------------------------------------------------- Weitentaaal · 2022-01-27 07:55 Thank You guys for your help, works perfectly now