Board index » delphi » How do I measure width of a string

How do I measure width of a string

I want to set the column width of a StringGrid to fit the width of the
heading.  How do I determine the width of the string in order to do
this?

Eagerly awaiting a reply,
Lester

 

Re:How do I measure width of a string


Lester Hanger <lest...@global.co.za> wrote in article
<573445$...@osprey.global.co.za>...

Quote
> I want to set the column width of a StringGrid to fit the width of the
> heading.  How do I determine the width of the string in order to do
> this?

> Eagerly awaiting a reply,
> Lester

Try:

nPixels := StringGrid1.Canvas.TextWidth('Your text');

should work.  The TextWidth method returns the width in pixels of the
string passed in Text when rendered in the current font.

Randy Edwards

Re:How do I measure width of a string


Quote
Lester Hanger wrote:

> I want to set the column width of a StringGrid to fit the width of the
> heading.  How do I determine the width of the string in order to do
> this?

> Eagerly awaiting a reply,
> Lester

I haven't tried this, but the helpfile indicates that you use the
TextWidth method of the stringgrid's canvas property. So, for example,
to set the width of all columns in a grid, based on the text already in
the first row, you would do this:

.
.
const ofs: Integer = 4; {a pad factor to add ofs pixels to the column
width}
var idx: Integer;
.
begin
.
  with myGrid do
    for idx := 0 to ColCount - 1 do
      ColWidths[idx] := Canvas.TextWidth(Cells[idx,0]) + ofs;
.
.
end;

The method Canvas.TextWidth ultimately uses the Windows API function
GetTextExtentPoint, which takes into account intercharacter spacing and
I _think_ also takes into account kerning.

You can experiment with a value for 'ofs', but I found 4 to be fine ...
leaves a visually pleasing gap between columns. A value of zero results
in some of the heading text being clipped. This is because when the text
is drawn in the cell, a (fairly standard) offset of 2 pixels from the
left edge is used. If you use ofs := 2, you compensate for this, but the
right edge butts right against the end of the text. Using ofs := 4
leaves the same gap on the right edge as the component itself puts on
the left edge.
--
Regards, Stephen Walter

Re:How do I measure width of a string


Use the Length function.
Hope this helps.

Lester Hanger <lest...@global.co.za> wrote in article
<573445$...@osprey.global.co.za>...

Quote
> I want to set the column width of a StringGrid to fit the width of the
> heading.  How do I determine the width of the string in order to do
> this?

> Eagerly awaiting a reply,
> Lester

Re:How do I measure width of a string


On Thu, 21 Nov 1996 22:49:31 GMT, lest...@global.co.za (Lester Hanger)
wrote:

Quote
>I want to set the column width of a StringGrid to fit the width of the
>heading.  How do I determine the width of the string in order to do
>this?

>Eagerly awaiting a reply,
>Lester

Canvas.Textwidth('String');

Re:How do I measure width of a string


Quote
> lest...@global.co.za (Lester Hanger) wrote in article

<573445$...@osprey.global.co.za>...

Quote
> I want to set the column width of a StringGrid to fit the width of the
> heading.  How do I determine the width of the string in order to do
> this?

Try
Length(String) ;

Re:How do I measure width of a string


Ken Carrick <kgcar...@mbay.net> wrote in article
<01bbdca9.62b910e0$58277...@mercury.as.nps.navy.mil>...

Quote

> > lest...@global.co.za (Lester Hanger) wrote in article
> <573445$...@osprey.global.co.za>...
> > I want to set the column width of a StringGrid to fit the width of the
> > heading.  How do I determine the width of the string in order to do
> > this?

> Try
> Length(String) ;

Hi Ken,

I think you misunderstood Lester's question. I believe the functionality
he's looking for is Canvas.TextWidth( String );

Other Threads