Autosize the columns and rows in MSFlexGrid

来源:互联网 发布:纳智捷车子怎么样 知乎 编辑:程序博客网 时间:2024/05/01 01:39

Public Function FG_AutosizeRows(myGrid As MSFlexGrid, _
                                Optional ByVal lFirstRow As Long = -1, _
                                Optional ByVal lLastRow As Long = -1, _
                                Optional bCheckFont As Boolean = False)

    Dim lCol As Long, lRow As Long, lCurCol As Long, lCurRow As Long
    Dim lCellHeight As Long, lRowHeight As Long
    Dim bFontBold As Boolean
    Dim dFontSize As Double
    Dim sFontName As String
    If bCheckFont Then
        bFontBold = Me.FontBold
        sFontName = Me.FontName
        dFontSize = Me.FontSize
    End If
    With myGrid
        If bCheckFont Then
            lCurCol = .Col
            lCurRow = .Row
        End If
        If lFirstRow = -1 Then lFirstRow = 0
        If lLastRow = -1 Then lLastRow = .Rows - 1
        For lRow = lFirstRow To lLastRow
            lRowHeight = 0
            If bCheckFont Then .Row = lRow
            For lCol = 0 To .Cols - 1
                If bCheckFont Then
                    .Col = lCol
                    Me.FontBold = .CellFontBold
                    Me.FontName = .CellFontName
                    Me.FontSize = .CellFontSize
                End If
                lCellHeight = Me.TextHeight(.TextMatrix(lRow, lCol))
                If lCellHeight > lRowHeight Then lRowHeight = lCellHeight
            Next lCol
            .RowHeight(lRow) = lRowHeight + Me.TextHeight("Wg") / 5
        Next lRow
        If bCheckFont Then
            .Row = lCurRow
            .Col = lCurCol
        End If
    End With
    If bCheckFont Then
        Me.FontBold = bFontBold
        Me.FontName = sFontName
        Me.FontSize = dFontSize
    End If
End Function
Public Function FG_AutosizeCols(myGrid As MSFlexGrid, _
                                Optional ByVal lFirstCol As Long = -1, _
                                Optional ByVal lLastCol As Long = -1, _
                                Optional bCheckFont As Boolean = False)
    Dim lCol As Long, lRow As Long, lCurCol As Long, lCurRow As Long
    Dim lCellWidth As Long, lColWidth As Long
    Dim bFontBold As Boolean
    Dim dFontSize As Double
    Dim sFontName As String
    If bCheckFont Then
        ' save the forms font settings
        bFontBold = Me.FontBold
        sFontName = Me.FontName
        dFontSize = Me.FontSize
    End If
    With myGrid
        If bCheckFont Then
            lCurRow = .Row
            lCurCol = .Col
        End If
        If lFirstCol = -1 Then lFirstCol = 0
        If lLastCol = -1 Then lLastCol = .Cols - 1
        For lCol = lFirstCol To lLastCol
            lColWidth = 0
            If bCheckFont Then .Col = lCol
            For lRow = 0 To .Rows - 1
                If bCheckFont Then
                    .Row = lRow
                    Me.FontBold = .CellFontBold
                    Me.FontName = .CellFontName
                    Me.FontSize = .CellFontSize
                End If
                lCellWidth = Me.TextWidth(.TextMatrix(lRow, lCol))
                If lCellWidth > lColWidth Then lColWidth = lCellWidth
            Next lRow
            .ColWidth(lCol) = lColWidth + Me.TextWidth("W")
        Next lCol
        If bCheckFont Then
            .Row = lCurRow
            .Col = lCurCol
        End If
    End With
    If bCheckFont Then
        ' restore the forms font settings
        Me.FontBold = bFontBold
        Me.FontName = sFontName
        Me.FontSize = dFontSize
    End If
End Function
例:
Private Sub Command1_Click()
    Call FG_AutosizeRows(MSFlexGrid1, -1, -1, True)
End Sub
Private Sub Command2_Click()
    Call FG_AutosizeCols(MSFlexGrid1, -1, -1, True)
End Sub
Private Sub Form_Load()
    Dim fso As New FileSystemObject
    Dim txtf As TextStream
    Dim path As String, Line As String
    Dim Cells() As String, Idx As Integer
    path = App.path
    If Right(path, 1) <> "/" Then path = path & "/"
    Set txtf = fso.OpenTextFile(path & "score.txt", ForReading)
    With MSFlexGrid1
        .AllowUserResizing = flexResizeColumns ' 可以改变列宽
        .Cols = 5       ' 列数 = 5
        .Rows = 1       ' 行数暂订 1
        .FixedCols = 0  ' 固定列 = 0
        ' 标题头
        .TextMatrix(0, 0) = "学号"
        .TextMatrix(0, 1) = "姓名"
        .TextMatrix(0, 2) = "语文"
        .TextMatrix(0, 3) = "数学"
        .TextMatrix(0, 4) = "英语"
        .ColAlignment(0) = flexAlignRightCenter
        .ColAlignment(2) = flexAlignRightCenter
        .ColAlignment(3) = flexAlignRightCenter
        .ColAlignment(4) = flexAlignRightCenter
    End With
    Idx = 1
    While Not txtf.AtEndOfStream
        Line = txtf.ReadLine
        Cells = Split(Line, ",")
        MSFlexGrid1.Rows = MSFlexGrid1.Rows + 1 ' 增一行
        ' 将Cells(0,4) 指定给TextMatrix(Idx, 0,4)
        For I = 0 To UBound(Cells)
            MSFlexGrid1.TextMatrix(Idx, I) = Cells(I)
        Next
        Idx = Idx + 1
    Wend
    txtf.Close
End Sub
'Refer to Microsoft Script Runtime
数据来自www.vbgood.com  score.txt
850301,陈桶一,90,76,98
850302,黄光权,58,77,75
850303,胡生妙,41,14,33
850304,王为全,95,97,87
850305,李日正,59,66,57
850306,刘德昌,28,11,33
850307,方正一,98,100,100
850308,刘康保,0,0,10
850309,谢掬花,95,74,89
850310,王美兰,41,46,49
850311,徐小当,91,99,84
850312,叶小毛,0,10,0
850313,林东海,92,92,88
850314,林大阳,35,19,29
850315,钟德级,98,90,91
850316,刘力锦,47,50,55
850317,方小房,94,94,100
850318,刘纪成,79,85,77
850319,李普三,84,74,58
850320,刘一心,54,57,64
850321,方日通,83,80,94
850322,刘顺成,71,65,73
850323,谢利利,20,25,10
850324,王涵合,93,100,90
850325,徐佳佳,68,56,39
850326,李红仁,25,24,0
850327,林大玉,87,77,92
850328,林玉成,64,90,59
850329,钟百达,0,0,20


Most samples in this complete thread can also be used for the MSHFlexGrid, in the functions just change the As MSFlexGrid to As MSHFlexGrid

原创粉丝点击