如何在VB6中计算flexgrid表的高度,使其仅包含已填充的行数。
当前
myFlexGrid.Height = (myFlexGrid.CellHeight * myFlexGrid.Rows) ' paraphrased from code
大约每行短3像素。加上魔术数字有点怪异,并且想要在不诉诸该密码的情况下完成此操作。
更新:为了使事情复杂化,还需要处理多行单元。
RS Coneley已经关闭,但这是说明所有DPI设置的正确方法:
Me.MSFlexGrid1.Height = Me.MSFlexGrid1.CellHeight _
* (Me.MSFlexGrid1.Rows + Me.MSFlexGrid1.FixedRows) _
+ (Screen.TwipsPerPixelY * 2)
您需要走
Me.MSFlexGrid1.Height = (Me.MSFlexGrid1.CellHeight) * (Me.MSFlexGrid1.Rows + _
Me.MSFlexGrid1.FixedRows) + 30
30将使其变长两个像素,以显示在flexgrid周围运行的黑色边框。
也禁用垂直滚动条也有帮助。
这是我想出的最终代码
For i = 0 To fgrComments.Rows - 1
'Set MSFlexGrid to appropriate Cell
myFlexGrid.Row = i
'Set textbox to match the selected cell
txtSizer.Width = myFlexGrid.ColWidth(2)
txtSizer.Font = myFlexGrid.Font
txtSizer.Text = myFlexGrid.Text
'Call API to determine how many lines of text are in text box
lLinesOfText = SendMessage(txtSizer.hwnd, EM_GETLINECOUNT, 0&, 0&)
' Update the running values
lTotalNumberOfRows = lTotalNumberOfRows + lLinesOfText
lCurrentHeight = lCurrentHeight + myFlexGrid.CellHeight
Next i
' resize the grid
Dim iSpacers As Integer
iSpacers = Screen.TwipsPerPixelY * lTotalNumberOfRows
myFlexGrid.Height = lCurrentHeight + iSpacers
您将需要声明SendMessage(see here to see how)和EM_GETLINECOUNT的值,但您应该可以自己这样做:-)
它不会删除魔术数字,但会合理化它们,对我来说足够接近。