Recent

Author Topic: TEdit.Autosize does not seem to do anything.  (Read 4035 times)

Avishai

  • Hero Member
  • *****
  • Posts: 1021
TEdit.Autosize does not seem to do anything.
« on: June 07, 2012, 01:40:43 pm »
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.
« Last Edit: June 07, 2012, 04:37:16 pm by Avishai »
Lazarus Trunk / fpc 2.6.2 / Win32

zeljko

  • Hero Member
  • *****
  • Posts: 1612
    • http://wiki.lazarus.freepascal.org/User:Zeljan
Re: TEdit.Autosize does not seem to do anything.
« Reply #1 on: June 07, 2012, 02:44:25 pm »
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

  • Hero Member
  • *****
  • Posts: 1021
Re: TEdit.Autosize does not seem to do anything.
« Reply #2 on: June 07, 2012, 02:54:38 pm »
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.
Lazarus Trunk / fpc 2.6.2 / Win32

Avishai

  • Hero Member
  • *****
  • Posts: 1021
Re: TEdit.Autosize does not seem to do anything.
« Reply #3 on: June 08, 2012, 03:36:52 pm »
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;
Lazarus Trunk / fpc 2.6.2 / Win32

CM630

  • Hero Member
  • *****
  • Posts: 1136
  • Не съм сигурен, че те разбирам.
    • http://sourceforge.net/u/cm630/profile/
Re: TEdit.Autosize does not seem to do anything.
« Reply #4 on: May 28, 2024, 03:37:32 pm »
Just to make sure,  is this still the best solution?

Actually, this does not work:

Code: Pascal  [Select][+][-]
  1.  
  2. procedure TForm1.Edit1EditingDone(Sender: TObject);
  3. begin
  4.   AdjustWidth(Sender);
  5. end;
  6.  
  7.  
  8. procedure TForm1.AdjustWidth(Sender: TObject);
  9. var
  10.   Label1 : tlabel;
  11. begin
  12.   Label1 := TLabel.Create(self);
  13.   Label1.AutoSize := True;
  14.   With TEdit(Sender) do begin
  15.     Label1.Caption := Text;
  16.     Width := Label1.Width;
  17.     Clear;
  18.     Text := Label1.Caption;
  19.     Label1.Caption := '';
  20.   end;
  21.   Label1.Free;
  22. end;  
« Last Edit: May 28, 2024, 03:45:40 pm by CM630 »
Лазар 3,4 32 bit (sometimes 64 bit); FPC3,2,2

jamie

  • Hero Member
  • *****
  • Posts: 6302
Re: TEdit.Autosize does not seem to do anything.
« Reply #5 on: May 28, 2024, 03:45:51 pm »
This works perfectly fine for me.

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Edit1Exit(Sender: TObject);
  2. begin
  3.   Edit1.Width := canvas.TextWidth(Edit1.TExt);
  4. end;                                            
  5.  
The only true wisdom is knowing you know nothing

CM630

  • Hero Member
  • *****
  • Posts: 1136
  • Не съм сигурен, че те разбирам.
    • http://sourceforge.net/u/cm630/profile/
Re: TEdit.Autosize does not seem to do anything.
« Reply #6 on: May 28, 2024, 03:58:23 pm »
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][+][-]
  1.   Canvas.Font.Size := Edit1.Font.Size;
  2.   Edit1.Width := canvas.TextWidth(Edit1.TExt) + 10;
« Last Edit: May 28, 2024, 04:04:18 pm by CM630 »
Лазар 3,4 32 bit (sometimes 64 bit); FPC3,2,2

jamie

  • Hero Member
  • *****
  • Posts: 6302
Re: TEdit.Autosize does not seem to do anything.
« Reply #7 on: May 28, 2024, 04:06:17 pm »
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][+][-]
  1. procedure TForm1.Edit1Exit(Sender: TObject);
  2. Var
  3.   C:TControlCanvas;
  4. begin
  5.   With TEdit(Sender)do
  6.   begin
  7.    C := TControlCanvas.Create;
  8.    C.Control := TEdit(Sender);
  9.    Width := C.TextWidth(Text)+C.TextWidth('X');
  10.    C.Free;
  11.   end;
  12. end;                              
  13.  

« Last Edit: May 28, 2024, 04:19:00 pm by jamie »
The only true wisdom is knowing you know nothing

CM630

  • Hero Member
  • *****
  • Posts: 1136
  • Не съм сигурен, че те разбирам.
    • http://sourceforge.net/u/cm630/profile/
Re: TEdit.Autosize does not seem to do anything.
« Reply #8 on: May 28, 2024, 04:23:16 pm »

Thanks, that seems to be the routine (X occurs to be too wide):
Code: Pascal  [Select][+][-]
  1. procedure AutosizeTEdit(Edit: TEdit)
  2. var  aCanvas : TControlCanvas
  3. begin  aCanvas := TControlCanvas.Create
  4.   aCanvas.Control := Edit
  5.   aCanvas.Font := Edit.Font
  6.   Edit.Width := aCanvas.TextWidth(Edit.Text) + aCanvas.TextWidth('I')
  7.   aCanvas.free
  8. end
  9.  




(Maybe aCanvas.Font:= Edit.Font
 is not useful, I have not tried without it yet).
« Last Edit: May 28, 2024, 04:25:17 pm by CM630 »
Лазар 3,4 32 bit (sometimes 64 bit); FPC3,2,2

jamie

  • Hero Member
  • *****
  • Posts: 6302
Re: TEdit.Autosize does not seem to do anything.
« Reply #9 on: May 28, 2024, 05:02:05 pm »
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][+][-]
  1. procedure TForm1.Edit1Exit(Sender: TObject);
  2. Var
  3.   C:TControlCanvas;
  4.   W:integer;
  5. begin
  6.   With TEdit(Sender)do
  7.   begin
  8.    C := TControlCanvas.Create;
  9.    W:= Width-ClientRect.Width;
  10.    If BorderStyle=bsSingle Then Inc(W,2);
  11.    C.Control := TEdit(Sender);
  12.    Width := C.TextWidth(Text)+W;
  13.    C.Free;
  14.   end;
  15. end;                                
  16.  
The only true wisdom is knowing you know nothing

ASerge

  • Hero Member
  • *****
  • Posts: 2279
Re: TEdit.Autosize does not seem to do anything.
« Reply #10 on: May 28, 2024, 11:19:51 pm »
Also need to remember about font.
Code: Pascal  [Select][+][-]
  1. uses LCLIntf, LCLType;
  2.  
  3. procedure AutoSizeEditWidth(Edit: TEdit);
  4. var
  5.   NewWidth: Integer;
  6.   AddToWidth: Integer;
  7.   C: TControlCanvas;
  8. begin
  9.   C := TControlCanvas.Create;
  10.   try
  11.     C.Control := Edit;
  12.     C.Font := Edit.Font;
  13.     NewWidth := C.TextWidth(Edit.Text);
  14.   finally
  15.     C.Free;
  16.   end;
  17.   AddToWidth := Edit.Width - Edit.ClientWidth;
  18.   if Edit.BorderStyle = bsSingle then
  19.     Inc(AddToWidth, GetSystemMetrics(SM_CXBORDER) * 2);
  20.   Edit.Width := NewWidth + AddToWidth;
  21. end;
  22.  
  23. procedure TForm1.Button1Click(Sender: TObject);
  24. begin
  25.   AutoSizeEditWidth(Edit1);
  26.   Edit1.SelStart := 0;
  27.   ActiveControl := Edit1;
  28. end;

CM630

  • Hero Member
  • *****
  • Posts: 1136
  • Не съм сигурен, че те разбирам.
    • http://sourceforge.net/u/cm630/profile/
Re: TEdit.Autosize does not seem to do anything.
« Reply #11 on: May 29, 2024, 07:48:40 am »
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][+][-]
  1. ..
  2.  
  3. finally
  4.     C.Free;
  5.   end;
  6.   if NewWidth < 0 then Exit;  
  7. ...

Лазар 3,4 32 bit (sometimes 64 bit); FPC3,2,2

 

TinyPortal © 2005-2018