vb—MSHFlexGrid控件总结(一)

前端之家收集整理的这篇文章主要介绍了vb—MSHFlexGrid控件总结(一)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

1、在MSHFlexGrid控件的一行中添加内容

方法一:用Tab字符分开不同单元格的内容
首先定义一个字符串的变量:row

<span style="font-size:18px;"><strong>'添加信息
   row = "学号" & vbTab & "姓名" & vbTab & "卡号" & vbTab & "金额" 
'传给MSHFlexGrid 
With MSHFlexGrid1
   .Rows = 0
   .AddItem row
   End With
</strong></span>

方法二:
<span style="font-size:18px;"><strong>With MSHFlexGrid1
      .Rows = 1
      .CellAlignment = 4
      .TextMatrix(0,0) = "学号"
      .TextMatrix(0,1) = "姓名"
      .TextMatrix(0,2) = "卡号"
      .TextMatrix(0,3) = "金额"
End With
</strong></span>

2、设置单行为浅灰,双行为浅黄色,颜色可以自己设定

<span style="font-size:18px;"><strong>Dim i As Integer 

With MSHFlexGrid1
        .AllowBigSelection = True ' 设置网格样式
        .FillStyle = flexFillRepeat
        For i = 0 To .Rows - 1
        .row = i: .col = .FixedCols
        .ColSel = .Cols() - .FixedCols - 1
        If i Mod 2 = 0 Then
        .CellBackColor = &HC0C0C0 ' 浅灰
        Else
        .CellBackColor = &HC0FFFF   ' 浅黄色
        End If
        Next i
    End With
</strong></span>

3、实现MSHF可以输入内容

需要一个textBox
新建一个textBox:text1

<span style="font-size:18px;"><strong>‘首先声明:
 Const ASC_ENTER = 13      '回车,作用是按回车触发可以输入文本内容
 Dim gRow As Integer
 Dim gCol As Integer


Private Sub Grid1_KeyPress(KeyAscii As Integer)
    ' Move the text Box to the current grid cell:
    Text1.Top = Grid1.CellTop + Grid1.Top
    Text1.Left = Grid1.CellLeft + Grid1.Left
    ' Save the position of the grids Row and Col for later:
    gRow = Grid1.Row
    gCol = Grid1.Col
    ' Make text Box same size as current grid cell:
    Text1.Width = Grid1.CellWidth - 2 * Screen.TwipsPerPixelX
    Text1.Height = Grid1.CellHeight - 2 * Screen.TwipsPerPixelY
    ' Transfer the grid cell text:
    Text1.Text = Grid1.Text
    ' Show the text Box:
    Text1.Visible = True
    Text1.ZOrder 0 ' 把 Text1 放到最前面!
    Text1.SetFocus
    ' Redirect this KeyPress event to the text Box:
    If KeyAscii <> ASC_ENTER Then
    SendKeys Chr$(KeyAscii)
    End If
End Sub

Private Sub Text1_KeyPress(KeyAscii As Integer)
    If KeyAscii = ASC_ENTER Then
        Grid1.SetFocus ' Set focus back to grid,see Text_LostFocus.
        KeyAscii = 0 ' Ignore this KeyPress.
    End If
End Sub

Private Sub Text1_LostFocus()
    Dim tmpRow As Integer
    Dim tmpCol As Integer
    ' Save current settings of Grid Row and col. This is needed only if
    ' the focus is set somewhere else in the Grid.
    tmpRow = Grid1.Row
    tmpCol = Grid1.Col
    ' Set Row and Col back to what they were before Text1_LostFocus:
    Grid1.Row = gRow
    Grid1.Col = gCol
    Grid1.Text = Text1.Text ' Transfer text back to grid.
    Text1.SelStart = 0 ' Return caret to beginning.
    Text1.Visible = False ' Disable text Box.
    ' Return row and Col contents:
    Grid1.Row = tmpRow
    Grid1.Col = tmpCol
End Sub
</strong></span>

4、单击某一行变色
<span style="font-size:18px;"><strong>public j as long

Private Sub RowColor(i As Long,j As Long)
'i代表现在点的这一行
Dim n As Long 'n代表列
With Me.Grid1
For n = 1 To .Cols - 1
.Row = i
.Col = n
.CellBackColor = &HC0FFFF


If j > 0 And i <> j Then
.Row = j
.CellBackColor = &HFFFFFF
End If
Next n
j = i
End With

End Sub

Private Sub Form_Load()
With Me.Grid1
.Cols = 5
.Rows = 6
End With
End Sub

Private Sub Grid1_Click()
Call RowColor(Me.Grid1.Row,j)
Debug.Print j
End Sub

</strong></span>

5、给单元格添加背景图片
<span style="font-size:18px;"><strong>​Set Grid1.CellPicture = LoadPicture("C:\Users\jf\Pictures\1.jpg")</strong></span>

猜你在找的VB相关文章