Autosize the columns and rows in MSFlexGrid

原创 2004年09月26日 14:47:00

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

columns列:Rows 工作表上所有的行

  • shy_snow
  • shy_snow
  • 2010年04月30日 09:07
  • 1010

HDU1401:Solitaire(BFS)

Problem Description Solitaire is a game played on a chessboard 8x8. The rows and columns of the che...
  • libin56842
  • libin56842
  • 2014年04月02日 23:43
  • 2307

2676_Sudoku

Sudoku Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 9679   Accepte...
  • cxb569262726
  • cxb569262726
  • 2012年03月10日 01:09
  • 1243

Converting Oracle rows to columns

Sometimes we need to write SQL that takes separate row results (on separate lines) and roll them tog...
  • caoguanling2011
  • caoguanling2011
  • 2015年04月10日 18:52
  • 402

C#:使用Column和Rows属性添加数据

通过设置DataGridView控件的Columns和Rows属性值,可以向数据控件DataGridView添加数据项,使其能够手动添加数据。   ►Columns属性    Columns属性...
  • vjudge
  • vjudge
  • 2014年07月28日 22:53
  • 1036

Lock rows and columns in Excel

You can view two areas of a worksheet and lock rows or columns in one area by splitting or freezing ...
  • cshun2005
  • cshun2005
  • 2007年12月24日 17:52
  • 767

Conversion of Oracle Columns into Rows

You Asked Hi Tom,  I have t1 table as follows  create table t1  (mnth varchar2(10),  col_1 nu...
  • caoguanling2011
  • caoguanling2011
  • 2015年04月10日 18:56
  • 241

剑指offer--面试题4:二维数组中的查找

#include bool Find(int *matrix, int rows, int columns, int number) { bool...
  • u010726692
  • u010726692
  • 2017年04月20日 14:21
  • 450

CSS3 网格布局(grid layout)基础知识 - 隐式网格自动布局(grid-auto-rows/grid-auto-columns/grid-auto-flow)

网格模板(grid-template)属性及其普通写法(longhands)定义了一个固定数量的轨道,构成显式网格。 当网格项目定位在这些界限之外,网格容器通过增加隐式网格线生成隐式网格轨道。 这...
  • iefreer
  • iefreer
  • 2016年01月20日 17:27
  • 2755

flash as3的TextFieldAutoSize自动适应与定位关系

经过测试,如果设置TextField的在xy(0,0),TextFieldAutoSize.LEFT就是左上角固定在原点,向右和下调整,如果还设置了width,就只能向下调整;对于center就比较郁...
  • qidizi
  • qidizi
  • 2013年03月28日 16:52
  • 1222
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Autosize the columns and rows in MSFlexGrid
举报原因:
原因补充:

(最多只允许输入30个字)