如何正确计算表的高度

时间:2023-02-01 08:29:23

How do you calculate the height of a flexgrid table in VB6 so that it only contains the number of filled rows.

如何在VB6中计算flexgrid表的高度,使其仅包含已填充行的数量。

Currently

myFlexGrid.Height = (myFlexGrid.CellHeight * myFlexGrid.Rows) ' paraphrased from code

comes out at about 3 pixels short per line. Adding in the magic number is a bit hackish and would like to accomplish this without having to resort to that.

每行约3个像素。添加幻数有点hackish并且想要实现这一点而不必诉诸于此。

Update: To complicate matters it also needs to handle multiline cells.

更新:为了使问题复杂化,它还需要处理多行单元格。

3 个解决方案

#1


RS Coneley is close, but here is the correct way that accounts for all DPI settings:

RS Coneley很接近,但这是解释所有DPI设置的正确方法:

Me.MSFlexGrid1.Height = Me.MSFlexGrid1.CellHeight _
                      * (Me.MSFlexGrid1.Rows + Me.MSFlexGrid1.FixedRows) _
                      + (Screen.TwipsPerPixelY * 2)

#2


You need to go

你需要去

Me.MSFlexGrid1.Height = (Me.MSFlexGrid1.CellHeight) * (Me.MSFlexGrid1.Rows + Me.MSFlexGrid1.FixedRows) + 30

Me.MSFlexGrid1.Height =(Me.MSFlexGrid1.CellHeight)*(Me.MSFlexGrid1.Rows + Me.MSFlexGrid1.FixedRows)+ 30

The 30 is to make it two pixels longer to show the black border running around the flexgrid.

30是使它长两个像素,以显示在flexgrid周围运行的黑色边框。

Also disabling the vertical scrollbar helps as well.

同时禁用垂直滚动条也有帮助。

#3


This is the final code I came up with

这是我提出的最终代码

    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

You will need to Declare the SendMessage (see here to see how) and the value of EM_GETLINECOUNT, but you should be able to do that yourself:-)

您将需要声明SendMessage(请参阅此处查看方法)和EM_GETLINECOUNT的值,但您应该能够自己执行此操作:-)

It doesn't remove the magic numbers but it does rationalise them which is close enough for me.

它没有删除神奇的数字,但它确实使它们合理化,这对我来说足够接近。

#1


RS Coneley is close, but here is the correct way that accounts for all DPI settings:

RS Coneley很接近,但这是解释所有DPI设置的正确方法:

Me.MSFlexGrid1.Height = Me.MSFlexGrid1.CellHeight _
                      * (Me.MSFlexGrid1.Rows + Me.MSFlexGrid1.FixedRows) _
                      + (Screen.TwipsPerPixelY * 2)

#2


You need to go

你需要去

Me.MSFlexGrid1.Height = (Me.MSFlexGrid1.CellHeight) * (Me.MSFlexGrid1.Rows + Me.MSFlexGrid1.FixedRows) + 30

Me.MSFlexGrid1.Height =(Me.MSFlexGrid1.CellHeight)*(Me.MSFlexGrid1.Rows + Me.MSFlexGrid1.FixedRows)+ 30

The 30 is to make it two pixels longer to show the black border running around the flexgrid.

30是使它长两个像素,以显示在flexgrid周围运行的黑色边框。

Also disabling the vertical scrollbar helps as well.

同时禁用垂直滚动条也有帮助。

#3


This is the final code I came up with

这是我提出的最终代码

    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

You will need to Declare the SendMessage (see here to see how) and the value of EM_GETLINECOUNT, but you should be able to do that yourself:-)

您将需要声明SendMessage(请参阅此处查看方法)和EM_GETLINECOUNT的值,但您应该能够自己执行此操作:-)

It doesn't remove the magic numbers but it does rationalise them which is close enough for me.

它没有删除神奇的数字,但它确实使它们合理化,这对我来说足够接近。