0

Given that I want my string grid to have equal height rows ...

I know that it will start with exactly 5 rows and I make it so at design time.

At run time I may want to add some extra rows (and I have code to not add below the bottom of the parent; at that point I rely on scroll bars).

It seems that sg.height := sg.height + sg.RowHeights[0]; is not producing the expected result - there is unexpecetd whitespace after the last row (I am still trying to calculate how the extra height corresponds to the number of rows, but it looks like it is the height of one row, no matter how many rows are in the grid).

Is there something else I should take into account? (perhaps GridLineWidth?) Or could I have set a property wrongly in the Object Inspector? Should I be using GridHeight, rather than Height?

Note: this problem only occurs when the stringgid is as wide as all cells and the scroll bar appears, drawing over part of the last column. If I leave an ugly blank sapce at the right of the string grid for the scroll bar to fit into, the problem does not occur.

Can anyone tell me the formula to calculate the new gridheight after adding a number of rows? Or wthere I am doing something wrong.


[Update] The fudge factor of 3 apears to correspond to the margin property

Mawg says reinstate Monica
  • 38,334
  • 103
  • 306
  • 551
  • Really? Are you talking about the Margins property which is an array of integers? It helps determine where the grid will be placed when used with the Align property. It is set to 3 by default. To determine the height of the grid if you have to include the Margins property you would have to include both the top and bottom and that would make 6 the number. I'm beginning to have more confidence in my conclusion that the grid height is determined by the total of the row heights plus the top border and bottom border plus the (unnamed) 1 pixel padding. Row heights include gridline thickness. – jrodenhi Nov 27 '12 at 22:46

2 Answers2

2

Just in case anyone other than me is interested, here's what I did to get it to work.

When ading a row :

newHeight := stringGrid.Height + 
             stringGrid.RowHeights[Pred(stringGrid.Row)] + 
             stringGrid.GridLineWidth;  

if newHeight < maxheight then
   stringGrid.Height := newHeight;

stringGrid.RowCount := stringGrid.RowCount + 1;

Alternatively, I can leave it until I have added all rows and then

stringGrid.Height := ((stringGrid.RowHeights[1] + stringGrid.GridLineWidth) *              
                      (stringGrid.VisibleRowCount + stringGrid.FixedRows)) + 3;
Mawg says reinstate Monica
  • 38,334
  • 103
  • 306
  • 551
  • 1
    Do you know why the extra 3 pixels are needed? Is it the same constant regardless of screen resolution? Is it possible to find those 3 pixels on an image of a String Grid? I'm only asking this because I have run into the same issue myself and, while I also used a constant adjustment, I don't know how well it works at other resolutions. – jrodenhi Oct 30 '12 at 16:52
  • I experimented until it looked good, but 4 and 5 looked good too ... sorry I can't help more – Mawg says reinstate Monica Nov 03 '12 at 05:45
  • 1
    I can't believe noone upped this when it's a perfect solution, +1 for useful to me – Kieran Wilson Nov 09 '12 at 00:00
  • +1 Thanks, @KieranWilson Btw, see update to the question to explain the mysterious "3" – Mawg says reinstate Monica Nov 20 '12 at 07:16
1

I will add my trial and error to your research in case it's of any value.
TMS AdvStringGrid. Here is an image of a TMS AdvStringGrid. I think it is largely based on an ordinary string grid with a lot of useful extensions. To make it easier to count rows, I magnified the image using SnagIt and made the pixel gridlines visible. I added some red lines to the top row of the grid. On this grid, the DefaultRowHeight is set to 21. Between the red lines, there are 5 pixels each so to get 21 pixels for the DefaultRowHeight you would have to include the stringgrid gridline which has a height of 1 pixel. When I load this grid, I use this code:

with gridLimits do
    begin
      RowCount := 3;
      Cells[0, 0] := 'Maximum Amount';
      Cells[0, 1] := 'Maximum Base';
      Cells[0, 2] := 'Maximum Period';
      IntegralHeight := False;
      MyHeight := 0;
      for i := 0 to RowCount - 1 do
        inc(MyHeight, RowHeights[i]);
      Height := MyHeight + 5;
    end;
end;

I captured the grid using SnagIt and let it capture an object on the screen. It included the 1 pixel grey line around the grid. The grid actually seems to visibly start at the blue line, so to get the height, I would think I would need to add the DefaultRowHeight times three plus the top blue line plus the bottom blue line, but if I do that and click on the bottom row, the grid scrolls up. It also scrolls up if I add 3 to the height. If I add 4 to the height, it does not scroll. I don't know why I set it on 5 and I will set it to 4 now because you can see that there is 1 pixel too much at the bottom.

It may be that the top and bottom grey lines should be added to get the total height of the grid and that is why it is the DefaultRowHeight times 3 plus the top and bottom lines plus two more for the grey lines that SnagIt included.

jrodenhi
  • 2,237
  • 1
  • 21
  • 31