VB3 How to Size the Rows & Columns of a Grid to Fit ExactlyLast reviewed: January 9, 1997Article ID: Q112861 |
The information in this article applies to:
- Professional Edition of Microsoft Visual Basic Programming System for Windows, version 2.0- Standard and Professional Editions of Microsoft Visual Basic Programming System for Windows, version 3.0
SUMMARYThis 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 INFORMATIONTo 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) \ 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. 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 nRowsThe 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
|
KBCategory: kbprg kbcode
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |