Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How do I get the width of TEDIT.TEXT?

868 views
Skip to first unread message

Jim Schoen

unread,
Oct 21, 1999, 3:00:00 AM10/21/99
to
Using D4.

I'm trying to find the width of a tedit.text field. I do not have a canvas.
I've been investigating the GETTEXTEXTENTPOINT function, but then I get
stuck on know what the HDC is for a CLASS (TFORM).

I just have a form that has a bunch of TEDITs on it. The values in the
TEDIT.TEXT can change and I want to expand the width of the box if need be.
But to do that I need to be able to check what the width is.

Is there a way I'm missing that will make this easier?

Thanks in advance.

Ralph Friedman (TeamB)

unread,
Oct 21, 1999, 3:00:00 AM10/21/99
to
In message <7uni1u$5f...@forums.borland.com>, Jim Schoen stated:

> I'm trying to find the width of a tedit.text field. I do not have a canvas.
> I've been investigating the GETTEXTEXTENTPOINT function, but then I get
> stuck on know what the HDC is for a CLASS (TFORM).
>
Assuming you're not using it for anything else, you can use the Form's Canvas
for this. Just make sure to set the Canvas font the same as the TEdit font and
use the TextWidth function of the Canvas or you can pass Canvas.Handle to any
API that requires an HDC.
--
Regards
Ralph (TeamB)
--


LJ

unread,
Oct 21, 1999, 3:00:00 AM10/21/99
to
>I just have a form that has a bunch of TEDITs on it. The values in the
>TEDIT.TEXT can change and I want to expand the width of the box if need be.
>But to do that I need to be able to check what the width is.


You can use any canvas. Your form has one. If the form's font is the same as
the tedit, then it's no problem...

LB Cid <swe>


BillB

unread,
Oct 21, 1999, 3:00:00 AM10/21/99
to
Jim Schoen wrote in message <7uni1u$5f...@forums.borland.com>...
>Using D4.

>
>I'm trying to find the width of a tedit.text field. I do not have a
canvas.
>I've been investigating the GETTEXTEXTENTPOINT function, but then I get
>stuck on know what the HDC is for a CLASS (TFORM).
>
Jim, the following example may be usefull in answer to GetTextExtentPoint.
Regards..Bill

{GetTextExtentPoint is provided for compatibility with 16-bit versions of
Windows.
Win32-based applications should call the GetTextExtentPoint32 function,
which provides more accurate results. }

procedure TForm1.Button2Click(Sender: TObject);
var
DC: HDC;
TextSize: TSize;
SaveFont: HFont;
begin
DC := GetDC(0);
SaveFont := SelectObject(DC, Edit1.Font.Handle);
GetTextExtentPoint32(DC, PChar(Edit1.Text),
Length(Edit1.Text), TextSize);
SelectObject(DC, SaveFont);
ShowMessage(Format('Height= %d Width = %d', [TextSize.cy, TextSize.cx]));
ReleaseDC(0, DC);
end;

Then set Edit1.Width to TextSize.cx plus the border.

Jon Perry

unread,
Oct 22, 1999, 3:00:00 AM10/22/99
to
Why can't you just take the length of the text in the edit box
(length(edit1.text)), multiply that by the font size, etc.., and adjust
accordingly?


--
Jon Perry
pe...@globalnet.co.uk

Jim Schoen <Inp...@qsi-r2.com> wrote in message
news:7uni1u$5f...@forums.borland.com...


> Using D4.
>
> I'm trying to find the width of a tedit.text field. I do not have a
canvas.
> I've been investigating the GETTEXTEXTENTPOINT function, but then I get
> stuck on know what the HDC is for a CLASS (TFORM).
>

> I just have a form that has a bunch of TEDITs on it. The values in the
> TEDIT.TEXT can change and I want to expand the width of the box if need
be.
> But to do that I need to be able to check what the width is.
>

Mike Orriss (TeamB)

unread,
Oct 22, 1999, 3:00:00 AM10/22/99
to
In article <7uo8as$bu...@forums.borland.com>, Jon Perry wrote:
> Why can't you just take the length of the text in the edit box
> (length(edit1.text)), multiply that by the font size, etc.., and adjust
> accordingly?
>

Because it doesn't work for proportional fonts.

Mike Orriss (TeamB)
(Unless stated otherwise, my replies relate to Delphi 4.03/5.00)
(Unsolicited e-mail replies will most likely be ignored)


Jim Schoen

unread,
Oct 25, 1999, 3:00:00 AM10/25/99
to
Thank you all for replying. I didn't realize that I always have a canvas
when I have a form. (I thought I had to declare something as TCANVAS, but I
didn't have a need for any drawing components.). Thanks for making this
clear, because this makes it easy.

Bill, thanks for giving me the parameters for finding the HDC for
GETTEXTEXTENTPOINT32. I guess that I won't need to go through that if I can
tap into the Canvas and simply do a TFormVariable.CANVAS.TEXTWIDTH ('test
this line');

Jim

Ralph Friedman (TeamB) <ralphf...@email.com> wrote in message
news:VA.000005a...@della.garlin...


> In message <7uni1u$5f...@forums.borland.com>, Jim Schoen stated:

> > I'm trying to find the width of a tedit.text field. I do not have a
canvas.
> > I've been investigating the GETTEXTEXTENTPOINT function, but then I get
> > stuck on know what the HDC is for a CLASS (TFORM).
> >

0 new messages