Solved

# Dynamically setting col width in string grid

Posted on 2004-03-23
468 Views
I am trying to set the width of the columns in a string grid depending on the width of the data.

I can calculate the maximum number of chraracters used in any column. My questions are:-

1) if I use non proportional fonts how do I convert from font size to pitch in pixels?
2) if I use proportional spaced fonts how do I get the length of the actual text in pixells?

Thanks in anticipation

Alan
0
Question by:alanjbrown
• 3
• 2
• 2
• +1

LVL 17

Expert Comment

ID: 10656999
Canvas.TextWidth(str1) in both cases
0

LVL 1

Expert Comment

ID: 10657533
You will need to set the font first mokule.

use:

canvas.font.name := 'fontnamehere';
canvas.font.size := 14;
etc
then :
Canvas.TextWidth(str1)

Kristian
0

LVL 17

Expert Comment

ID: 10657716
No Kristian, u don't need, but u can
0

LVL 11

Expert Comment

ID: 10657981
mokule is right, there is no need to reset the fonts. They should already be set before the call to TextWidth, however, you can change them to something else, if you'd like, just before getting the width.

Shane
0

LVL 11

Accepted Solution

shaneholmes earned 125 total points
ID: 10658054
Here is an example using TextWidth.
This example iterates through the grid adjusting the colwidths

Shane

var
i, j, ColWidth, ThisCelllWidth : integer;
begin
with StringGrid1 do
begin
For j := 0 to (ColCount - 1) do begin
ColWidth := 0;
For i := 0 to (RowCount -1) do begin
ThisCellWidth := StringGrid1.Canvas.TextWidth(Cells[j,i])
If ThisCellWidth > ColWidth then ColWidth := ThisCellWidth;
end; {for all rows}
StringGrid1.ColWidths[j] := ColWidth + 2; {or + whatever margin you want}
end; {for all columns}
end; {with StringGrid1}

Shane
0

LVL 1

Expert Comment

ID: 10658079
just a force of habit,
our grids have different fonts in cells to titles, so i always set the font just as habit really to be sure.
K
0

LVL 1

Author Comment

ID: 10658416
Thank you all for the help
0

LVL 17

Expert Comment

ID: 10659800
Hi alanjbrown
Kind regards
Marek
0

LVL 1

Author Comment

ID: 10665443
Hi Marek

There was nothing wrong with your answer, however when I looked at all the answers received, Shane's answer was the most complete. I therefore decided to award him the points.

Regards
Alan
0

## Featured Post

### Suggested Solutions

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…