TEdit.Autosize does not seem to do anything. lazarus / 31-LCL ---------------------------------------------------------------------- Avishai · 2012-06-07 11:40 I need a TEdit that can Autosize in the OnExit.  Does anyone have a proper solution for this?  I have a 'dirty' workaround, but I do not like it.  Right now I copy TEdit.Text to a TLabel.Caption that is set to Autosize, then resize the TEdit.Width to TLabel.Width. In TEdit, I do not see any published properties that will allow me to get TextWidth. ---------------------------------------------------------------------- zeljko · 2012-06-07 12:44 I think that you're wrong about autosize of TEdit. It does not autosize width, only height. So ,if you want to autosize width, then you have to do it on your own. ---------------------------------------------------------------------- Avishai · 2012-06-07 12:54 Mr. zeljko, you are correct.  TEdit does autosize Height for Font change.  I am looking for a way to properly autosize for width 'on my own'   I was hoping that someone had already done this and would like to share. ---------------------------------------------------------------------- Avishai · 2012-06-08 13:36 This is my workaround.  It works, but it is 'dirty'. procedure TForm1.AdjustWidth(Sender: TObject); begin   With TEdit(Sender) do begin     Label1.Caption:= Text;     Width:= Label1.Width;     Clear;     Text:= Label1.Caption;     Label.Caption:= '';   end; end; ---------------------------------------------------------------------- CM630 · 2024-05-28 13:37 Just to make sure,  is this still the best solution? Actually, this does not work: 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.Edit1EditingDone(Sender: TObject);begin  AdjustWidth(Sender);end;  procedure TForm1.AdjustWidth(Sender: TObject);var  Label1 : tlabel;begin  Label1 := TLabel.Create(self);  Label1.AutoSize := True;  With TEdit(Sender) do begin    Label1.Caption := Text;    Width := Label1.Width;    Clear;    Text := Label1.Caption;    Label1.Caption := '';  end;  Label1.Free;end; ---------------------------------------------------------------------- jamie · 2024-05-28 13:45 This works perfectly fine for me. 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.Edit1Exit(Sender: TObject);begin  Edit1.Width := canvas.TextWidth(Edit1.TExt);end; ---------------------------------------------------------------------- CM630 · 2024-05-28 13:58 Indeed, that is pretty close, but this is closer: Edit1.Width := canvas.TextWidth(Edit1.TExt) + 10; But maybe 10 will not be sufficient for all fonts, I will try. TEdit has some border spacings, but they seem to be ignored. Edit: No, it does not work. If I set the FontSize of the TEdit to 26 only 50% of the characters are shown. This seems to work so far: 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   Canvas.Font.Size := Edit1.Font.Size;  Edit1.Width := canvas.TextWidth(Edit1.TExt) + 10; ---------------------------------------------------------------------- jamie · 2024-05-28 14:06 when the border is on, "Single", there is ~2 pixels per side. The BorderWidth property reports 0 so can't use that. Also, the same font needs to be used as the TFORM here. You can create a TControlCanvas locally and get all the current infor for the EDIT there. This is a little cleaner; 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.Edit1Exit(Sender: TObject);Var  C:TControlCanvas;begin  With TEdit(Sender)do  begin   C := TControlCanvas.Create;   C.Control := TEdit(Sender);   Width := C.TextWidth(Text)+C.TextWidth('X');   C.Free;  end;end; ---------------------------------------------------------------------- CM630 · 2024-05-28 14:23 Thanks, that seems to be the routine (X occurs to be too wide):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 AutosizeTEdit(Edit: TEdit)var  aCanvas : TControlCanvasbegin  aCanvas := TControlCanvas.Create  aCanvas.Control := Edit  aCanvas.Font := Edit.Font  Edit.Width := aCanvas.TextWidth(Edit.Text) + aCanvas.TextWidth('I')  aCanvas.freeend  (Maybe aCanvas.Font:= Edit.Font  is not useful, I have not tried without it yet). ---------------------------------------------------------------------- jamie · 2024-05-28 15:02 I like this one, it seems to be a little accurate. It accounts for the border line and also it does a clientRect measurement from the Width of the control 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.Edit1Exit(Sender: TObject);Var  C:TControlCanvas;  W:integer;begin  With TEdit(Sender)do  begin   C := TControlCanvas.Create;   W:= Width-ClientRect.Width;   If BorderStyle=bsSingle Then Inc(W,2);   C.Control := TEdit(Sender);   Width := C.TextWidth(Text)+W;   C.Free;  end;end; ---------------------------------------------------------------------- ASerge · 2024-05-28 21:19 Also need to remember about font. 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 uses LCLIntf, LCLType; procedure AutoSizeEditWidth(Edit: TEdit);var  NewWidth: Integer;  AddToWidth: Integer;  C: TControlCanvas;begin  C := TControlCanvas.Create;  try    C.Control := Edit;    C.Font := Edit.Font;    NewWidth := C.TextWidth(Edit.Text);  finally    C.Free;  end;  AddToWidth := Edit.Width - Edit.ClientWidth;  if Edit.BorderStyle = bsSingle then    Inc(AddToWidth, GetSystemMetrics(SM_CXBORDER) * 2);  Edit.Width := NewWidth + AddToWidth;end; procedure TForm1.Button1Click(Sender: TObject);begin  AutoSizeEditWidth(Edit1);  Edit1.SelStart := 0;  ActiveControl := Edit1;end; ---------------------------------------------------------------------- CM630 · 2024-05-29 05:48 Indeed, it does not work without setting the font. I just added if NewWidth < 0 then Exit; in case something bad happens while assigning the font (I have no idea what might happen). 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 .. finally    C.Free;  end;  if NewWidth < 0 then Exit;   ...