VB6-表格控件MSHFlexGrid 實用代碼


在vb6中要顯示數據雖然有datagrid、msflexgrid、mshflexgrid、vsflexgrid、True dbgrid7.0 可選,不過我在工作中用的最多的還是MSHFlexGrid,以下我會常分享一些使用這個控件的技巧、方法代碼,保證拿了就可以用。

 

1、使用MSHFlexGrid的FormatString屬性可以做到一次設置行標題和列標題
 
Dim sTitle As String
 
sTitle = "<Name |<Address |<Telephone |<Social Security>"
sTitle = sTitle + ";|Robert|Jimmy|Bonzo|John Paul"
 
MSHFlexGrid.FormatString = sTitle
 
 
    Dim sTitle As String  '列標題
    '設置數據源
    Set msh_Data.DataSource = AllRs
    msh_Data.Refresh
 
    '///解決不能單擊鼠標指向行///
    If msh_Data.Rows > 1 Then
        msh_Data.FixedRows = 0
        msh_Data.FixedRows = 1
    End If
    
    With msh_Data
    
        '填充左邊記錄行數
        .TextMatrix(0, 0) = " 序號"
        
        Dim i As Long
        For i = .FixedRows To .Rows - .FixedRows
            .TextMatrix(i, 0) = i
        Next i
        
        .RowHeight(0) = 600  '設置首行也即標題欄高度
        
        '單擊選擇整行
        .FocusRect = flexFocusNone
        .SelectionMode = flexSelectionByRow
        'msh_Data.BackColorSel = vbYellow
        '固定第一列,不然再移動下一列的時候就會自動跳到最后一列了
        .Col = 1
        .FormatString = sTitle  '設置列標題內容
        .ColWidth(0) = 0
        .ColWidth(1) = 600
        
        
    End With
 
    '刷新后選取首行
    msh_Data.Row = msh_Data.FixedRows
    msh_Data.RowSel = msh_Data.FixedRows
    msh_Data.Col = 0
    msh_Data.ColSel = msh_Data.Cols - 1
 
     '刷新后選取最后一行
    MSFlexGrid1.Row = MSFlexGrid1.Rows - MSFlexGrid1.FixedRows
    MSFlexGrid1.RowSel = MSFlexGrid1.Rows - MSFlexGrid1.FixedRows
    MSFlexGrid1.Col = 0
    MSFlexGrid1.ColSel = MSFlexGrid1.Cols - 1
 
 

'禁止mshflexgrid選擇多行記錄

'第一種方法
Private Sub MSHFlexGrid1_MouseMove(Button As Integer, Shift As Integer, x As Single, y As Single)
  If MSHFlexGrid1.RowSel <> MSHFlexGrid1.Row Then MSHFlexGrid1.RowSel = MSHFlexGrid1.Row
End Sub

'第二種方法

Private Sub msh_Data_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)

If msh_Data.MouseRow = 0 Then Exit Sub

With msh_Data
.Row = .MouseRow
CURRENTROW = .Row
.Col = 0 '如果是0則可以不選擇多行
.ColSel = .Cols - 1
End With

End Sub

Private Sub msh_Data_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)

If msh_Data.MouseRow = 0 Then Exit Sub

With msh_Data
.RowSel = CURRENTROW
.ColSel = .Cols - 1
End With

End Sub

 
 
 
 
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM