Recent

Author Topic: Tedit control that adjusts its width to the length of the text  (Read 1207 times)

simsee

  • Full Member
  • ***
  • Posts: 240
As in the title, I need a tedit control that adjusts its width to the length of the text. I currently use the TEdit.OnChange event, whose handler is as follows:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Edit1Change(Sender: TObject);
  2. begin
  3.   Edit1.Width:=Canvas.TextWidth(Edit1.Text)+10;
  4. end;

This solution isn't very satisfactory. Is there a better one?

Thanks in advance.

rvk

  • Hero Member
  • *****
  • Posts: 7042
Re: Tedit control that adjusts its width to the length of the text
« Reply #1 on: July 12, 2025, 11:49:34 pm »
This solution isn't very satisfactory. Is there a better one?
Maybe first tell us why it's not satisfactory.
Also mention your target OS.

simsee

  • Full Member
  • ***
  • Posts: 240
Re: Tedit control that adjusts its width to the length of the text
« Reply #2 on: July 12, 2025, 11:54:22 pm »
For example, with the current version of Lazarus on Windows, the first character may be partially visible, as in the attached screenshot. I'm looking for a cross-platform solution.

rvk

  • Hero Member
  • *****
  • Posts: 7042
Re: Tedit control that adjusts its width to the length of the text
« Reply #3 on: July 13, 2025, 12:03:16 am »
It seems like the + 10 is not sufficient.
You have some extra white space behind the string so you at least need to account for 1 character extra.

You could try
Canvas.TextWidth(Edit1.Text + 'A') + 10

simsee

  • Full Member
  • ***
  • Posts: 240
Re: Tedit control that adjusts its width to the length of the text
« Reply #4 on: July 13, 2025, 12:10:02 am »
In reality, the characters you see are the only ones typed at runtime, including the partially visible initial 'A'.


jamie

  • Hero Member
  • *****
  • Posts: 7761
Re: Tedit control that adjusts its width to the length of the text
« Reply #5 on: July 13, 2025, 12:22:00 am »
Edit controls always need extra space.

Code: Pascal  [Select][+][-]
  1.   With TEdit(Sender) do
  2.    Begin
  3.      ClientWidth := Canvas.TextWidth(Text)+(Canvas.TextWidth('W') shl 1);
  4.    end;                                      
  5.  
  6.  
The only true wisdom is knowing you know nothing

rvk

  • Hero Member
  • *****
  • Posts: 7042
Re: Tedit control that adjusts its width to the length of the text
« Reply #6 on: July 13, 2025, 12:22:24 am »
In reality, the characters you see are the only ones typed at runtime, including the partially visible initial 'A'.
So? Principle is the same. The + 10 you supply isn't enough for the white space after the cursor. So you shouldn't set the width to exactly the width of the string. You need to add some space.

Adding a 'A' in the textwidth call adds the space for a A. You van also use B or something else as long as the string for textwidth is 1 character more than your actual string.

You can also do + 30 or whatever but then it's too wide when you have a smaller font. Using + 'A' makes sure you only add as much as you need for one character.

simsee

  • Full Member
  • ***
  • Posts: 240
Re: Tedit control that adjusts its width to the length of the text
« Reply #7 on: July 13, 2025, 12:36:13 am »
Now I understand. Thank you very much.

 

TinyPortal © 2005-2018