2009-04-22 76 views
0

如何計算VB6中的flexgrid表格的高度,使其僅包含填充行的數量。如何正確計算表格的高度

目前

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

在大約3個像素,每行簡短出來。添加魔術數字有點駭人聽聞,並希望在不必訴諸於此的情況下完成此操作。

更新: 複雜的事情,它也需要處理多行單元格。

回答

1

你需要去

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

30是使它延長了兩個像素以顯示圍繞柔性網格運行的黑色邊框。

同樣禁用垂直滾動條也有幫助。

+0

其實30從我能找到不正確:如果用戶在120dpi模式下運行,那麼12緹等於1像素。但是,這種添加一個半任意數字是我想要避免的。 – 2009-04-22 13:26:02

2

RS Coneley接近,但在這裏是佔所有DPI設置的正確方法:

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

這是我想出了

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的值,但您應該可以自己做到這一點:-)

它不會刪除幻數,但它可以合理化這對我來說足夠接近。