MSHFlexGrid控制項自動調整列寬應用

來源:互聯網
上載者:User

Public Sub AutoSizeFlexGrid(flexObject As MSHierarchicalFlexGridLib.MSHFlexGrid)
Dim RsFlex As ADODB.Recordset, sngMaxWidth() As Single, sngTextWidth As Single
Dim x As Integer, y As Integer

With flexObject
    If .Cols = 0 Then Exit Sub
    .Visible = False
    ReDim sngMaxWidth(.Cols - 1)
    For y = 0 To .Rows - 1
        For x = 0 To .Cols - 1
            sngTextWidth = .Parent.TextWidth(Trim(.TextMatrix(y, x))) * 1
            If sngMaxWidth(x) < sngTextWidth Then sngMaxWidth(x) = sngTextWidth
        Next
    Next
   
    For x = 0 To flexObject.Cols - 1
       .ColWidth(x) = sngMaxWidth(x) + 120
    Next
    For x = 0 To .Cols - 1
        Set RsFlex = flexObject.DataSource
        If x < RsFlex.Fields.Count And Not (RsFlex.EOF Or RsFlex.BOF) Then
           Select Case TypeName(RsFlex.Fields(x).Value)
             Case "Integer", "Double", "Currency", "Byte", "Long", "Decimal", "Single"
                  .ColAlignment(x) = flexAlignRightCenter
             Case Else
                  .ColAlignment(x) = flexAlignLeftCenter
           End Select
        End If
    Next
    .Row = 0
    .Col = 0
    If .Rows > 1 Then .Row = 1
    .ColSel = .Cols - 1
    .Visible = True
End With
End Sub

 

Public Sub AdjustColWidth(frmCur As Form, gridCur As Object, Optional bNullRow As Boolean = True, Optional dblIncWidth As Double = 0)
'--------------------------------------------------------------------
'功能:
' 自動調整Grid各列列寬為最合適的寬度
'參數:
' [frmCur].........................................當前工作表單
' [gridCur]........................................當前要調整的Grid
'--------------------------------------------------------------------
Dim i, j As Integer
Dim dblWidth As Double
With gridCur
For i = 0 To .Cols - 1
dblWidth = 0
If .ColWidth(i) <> 0 Then
For j = 0 To .Rows - 1
If frmCur.TextWidth(.TextMatrix(j, i)) > dblWidth Then
dblWidth = frmCur.TextWidth(.TextMatrix(j, i))
End If
Next
.ColWidth(i) = dblWidth + dblIncWidth + 100
End If
Next
End With

End Sub

最後如果你使用的是VS2003或以上版本,只要用以下方法實現,VB6可以用上面的方法

你可以這樣做設定MSHFLEXGRID的AllowUserResizing 屬性
為flexResizeColumns 就可以調整列的寬度了,讓隱藏的文字顯示出來

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.