Article ID: 112861
Article Last Modified on 12/9/2003
PixelRowHeight = (Grd.Height - 2) \ nRowsThe formula subtracts two pixels from the grid height to take into account the top and bottom border. Then it divides the result by the number of rows to get the height in pixels.
PixelsRemaining = (Grd.Height - 2) Mod nRowsThe example distributes one pixel of the remaining pixels to the height of each row until it runs out of pixels.
Name Properties Value ------------------------------- Text1 Text 2 Text2 Text 2 Command1 Caption Resize Grid1 ScrollBars 0-None
' Enter the following two lines as one, single line:
Sub SizeGrid (Frm As Form, Grd As Grid, nRows As Integer,
nCols As Integer)
Dim FormScaleMode As Integer 'Used to save ScaleMode
'Save ScaleMode of form and change to pixels:
FormScaleMode = Frm.ScaleMode
Frm.ScaleMode = 3 'Pixels
'Determine the height of a row in pixels and the remaining pixels:
PixelRowHeight = (Grd.Height - 2) \ nRows
PixelsRemaining = (Grd.Height - 2) Mod nRows
'Set the height of each column:
Grd.Rows = nRows
For i = 0 To nRows - 1
If i < PixelsRemaining Then
'Set the height of a row:
'One pixel is added to eat up remainder and get a perfect fit.
Grd.RowHeight(i) = PixelRowHeight * Screen.TwipsPerPixelY
Else
'Set the height of a row
Grd.RowHeight(i) = (PixelRowHeight - 1) * Screen.TwipsPerPixelY
End If
Next
'Determine the width of a column and the remaining pixels:
PixelColWidth = (Grd.Width - 2) \ nCols
PixelsRemaining = (Grd.Width - 2) Mod nCols
'Set the width of each column:
Grd.Cols = nCols
For i = 0 To nCols - 1
If i < PixelsRemaining Then
'Set the width of a column:
'One pixel is added to eat up remainder and get a perfect fit
Grd.ColWidth(i) = PixelColWidth * Screen.TwipsPerPixelX
Else
'Set the width of a column:
Grd.ColWidth(i) = (PixelColWidth - 1) * Screen.TwipsPerPixelX
End If
Next
'Return form to original ScaleMode:
Frm.ScaleMode = FormScaleMode
End Sub
Sub Command1_Click ()
Call SizeGrid(Form1, Grid1, CInt(Text1), CInt(Text2))
End Sub
Additional query words: 2.00 3.00
Keywords: kbcode KB112861