Solved

Dynamically setting col width in string grid

Posted on 2004-03-23
9
471 Views
Last Modified: 2010-04-05
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
Comment
Question by:alanjbrown
  • 3
  • 2
  • 2
  • +1
9 Comments
 
LVL 17

Expert Comment

by:mokule
ID: 10656999
Canvas.TextWidth(str1) in both cases
0
 
LVL 1

Expert Comment

by:Kristian
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

by:mokule
ID: 10657716
No Kristian, u don't need, but u can
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 11

Expert Comment

by:shaneholmes
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

by:
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

by:Kristian
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

by:alanjbrown
ID: 10658416
Thank you all for the help
0
 
LVL 17

Expert Comment

by:mokule
ID: 10659800
Hi alanjbrown
Could You please answer what was wrong in my answer to You question.
Kind regards
Marek
0
 
LVL 1

Author Comment

by:alanjbrown
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

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question