Board index » delphi » adjusting edit to width of text

adjusting edit to width of text

I'm looking for an easy way to do the following:  The user enters text
into an edit control, and the width of the control adjusts to equal the
width of the text entered.  Does an edit property or event do that?
I've got a quick and dirty fix:  put an invisible label on the form, on
each keyup for the edit set the label caption equal to the text (at that
point) in the edit, then set the width of the edit control equal to the
width of that label (plus a litttle).  It works, but surely Delphi
offers something more elegant.

Thanks, BC

 

Re:adjusting edit to width of text


Yes, there is.  You can use the TextWidth method of the TCanvas object to
get the width of a certain string.  You could have the following event
handler:

procedure TForm1.Edit1OnChange(Sender: TObject);
begin
  Edit1.Width := Canvas.TextWidth(Edit1.Text) + 16;
end;

You have to use the Canvas of the form, since TEdit doesn't have a Canvas
property (because it doesn't descend from TGraphicControl).  So, you have to
have the Font property of the form the same as the edit control, which is
most likely the case anyway if you haven't changed the Font of the edit
control.

Despite shielding you nicely from the winapi, Delphi can still manage to
have elegant code...

Cameron
________________________________
cameronm...@hotmail.com

Re:adjusting edit to width of text


Quote
Cameron McCormack wrote:
> Yes, there is.  You can use the TextWidth method of the TCanvas object to
> get the width of a certain string.  You could have the following event
> handler:

> procedure TForm1.Edit1OnChange(Sender: TObject);
> begin
>   Edit1.Width := Canvas.TextWidth(Edit1.Text) + 16;
> end;

> You have to use the Canvas of the form, since TEdit doesn't have a Canvas
> property (because it doesn't descend from TGraphicControl).  So, you have to
> have the Font property of the form the same as the edit control, which is
> most likely the case anyway if you haven't changed the Font of the edit
> control.

> Despite shielding you nicely from the winapi, Delphi can still manage to
> have elegant code...

> Cameron
> ________________________________
> cameronm...@hotmail.com

    Thanks!  Works like a charm.   Ben Crain

Re:adjusting edit to width of text


Quote
Cameron McCormack <cameronm...@hotmail.com> wrote:

: Yes, there is.
:
: procedure TForm1.Edit1OnChange(Sender: TObject);
: begin
:   Edit1.Width := Canvas.TextWidth(Edit1.Text) + 16;
: end;
:
: Despite shielding you nicely from the winapi, Delphi can still manage to
: have elegant code...

Cameron,

Why the hard-coded 16?  Trial and error?

Just curious about the details,
Mike

--
+------------------------+----------------------------------------+
| Michael J. Sussman     |    Looking for a fun game to play?     |
| Green Diamond Software |      Try DynaMine for free today!      |
| g...@primenet.com       | Check out http://www.primenet.com/~gds |
+------------------------+----------------------------------------+

Other Threads