自动调节MSHFlexGrid控件的行高列宽

做机房收费系统在做学生上机查看机上记录模块的时候,使用到了MSHFlexGrid这个控件。用这么控件显示数据的时候,遇到了小小的问题。程序运行到显示这个模块的界面的时候,数据总是显示不在界面内,但是导出Execel之后,数据显示是完整的。这是为什么呢?肯定是数据过长,导致该控件无法完整的显示数据。

后来从贾林那里淘来了一个函数。能够根据数据自动调整MSHFlexGrid控件的行高,列宽。

函数如下:

Option Explicit

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 As Integer
    Dim 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 j
                .ColWidth(i) = dblWidth + dblIncWidth + 100
            End If
        Next i
    End With
   
End Sub
函数的调用非常简单

例如:AdjustColWidth frmStCheckOnRs, MSHFlexGrid

 

posted on 2011-08-22 11:23  刘正权的博客  阅读(1227)  评论(0编辑  收藏  举报