Microsoft KB Archive/112861

= VB3 How to Size the Rows & Columns of a Grid to Fit Exactly =

Article ID: 112861

Article Last Modified on 12/9/2003

-

APPLIES TO


 * Microsoft Visual Basic 2.0 Professional Edition
 * Microsoft Visual Basic 2.0 Standard Edition
 * Microsoft Visual Basic 3.0 Professional Edition
 * Microsoft Visual Basic 3.0 Professional Edition

-



This article was previously published under Q112861



SUMMARY
This article explains how to size the rows and columns of a Visual Basic grid control to allow for the display of all cells in the grid within the current bounds of the grid. This technique is useful when the number of rows and columns in the grid changes dynamically and you need to see all the cells at once.



MORE INFORMATION
To allow all cells to be proportional and sized so that all can be seen within the current grid, you need to set the ScaleMode property of the grid's parent window to pixels. If the grid is on a form, the grid's parent window is the form. However, if the grid is nested within a picture box, then the picture box is the grid's parent window. Changing the parent's ScaleMode property to pixels changes the grid control's Height and Width properties to pixels. Once the grid's height and width are in pixels, it is possible to calculate the row height and column width in pixels. The example routine given below changes the form's ScaleMode to pixels temporarily, and then changes it back after it is done.

To calculate the height of a row in pixels, the example code uses this formula: PixelRowHeight = (Grd.Height - 2) \ nRows The 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.

To make the rows fit exactly into the grid, the example code calculates the remaining pixels. The number of remaining pixels is the remainder of the division used to calculate the row height. The remaining pixels are calculated by using this formula: PixelsRemaining = (Grd.Height - 2) Mod nRows The example distributes one pixel of the remaining pixels to the height of each row until it runs out of pixels.

Once you determine the height of a row in pixels, you can set the row height. The RowHeight property expects the height in twips. So it is necessary to multiply the pixel height by Screen.TwipsPerPixelY to convert to twips. The row height does not include the border, so one pixel must be subtracted from the row height prior to converting the height to twips and assigning it.

Similar methods are used to calculate the width of each row in pixels and assign it to the grid.

Step-by-Step Example
 Start a new project in Visual Basic. Form1 is created by default.  Add the following controls and set their properties using the following table as a guide:   Name       Properties   Value ---  Text1      Text         2 Text2     Text         2 Command1  Caption      Resize Grid1     ScrollBars   0-None   Put the following code in the General Declarations section of the form: ' 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   Add the following code to the command button's click event: Sub Command1_Click Call SizeGrid(Form1, Grid1, CInt(Text1), CInt(Text2)) End Sub  Save the project. Run the example.</li></ol>

Enter values for the number of rows and columns in the two text boxes and click the command button. The grid should have the exact number of rows and columns that you specified. Also, the rows and columns should fit exactly into the grid.

Additional query words: 2.00 3.00

Keywords: kbcode KB112861

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.