VB中MsFlexGrid控件的使用细则

>> 将文本赋值给MsFlexGrid的单元格
MsFlexGrid.TextMatrix(3,1)=”Hello”

>> 在MsFlexGrid控件单元格中插入背景图形
Set MsFlexGrid.CellPicture=LoadPicture(“C:/temp/1.bmp”)

>>选中某个单元
MsFlexGrid.Row=1
MsFlexGrid.Col=1

>>用粗体格式化当前选中单元
MsFlexGrid.CellFontBold=True

>> 添加新的一行
使用AddItem方法,用Tab字符分开不同单元格的内容

dim row as string
row=”AAA”&vbtab&”bbb”
MsFlexFrid1.addItem row


>>怎样来实现MSFlexGrid控件单数行背景为白色,双数的行背景为蓝色?
    Dim i As Integer
    With MSFlexGrid1
         .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 = vbBlue ’ 兰色
             End If
         Next i
     End With

>> MSFlexGrid控件如何移到最后一行
MSFlexGrid1.TopRow = MSFlexGrid1.Rows – 1

>>如何判断msflexgrid有无滚动条
Declare Function GetScrollRange Lib "user32" (ByVal hWnd As Long, ByVal nBar As Long, lpMinPos As Long, lpMaxPos As Long) As Long

Public Const SB_HORZ = &H0
Public Const SB_VERT = &H1

Public Function VsScroll(MshGrid As MSHFlexGrid) As Boolean           ’判断水平滚动条的可见性
Dim i As Long
VsScroll = False
i = GetScrollRange(MshGrid.hWnd, SB_HORZ, lpMinPos, lpMaxPos)
If lpMaxPos <> lpMinPos Then VsScroll = True
End Function

Public Function HeScroll(MshGrid As MSHFlexGrid) As Boolean           ’判断垂直滚动条的可见性
Dim i As Long
HeScroll = False
i = GetScrollRange(MshGrid.hWnd, SB_VERT, lpMinPos, lpMaxPos)
If lpMaxPos <> lpMinPos Then HeScroll = True
End Function

>>程序运行时,想动态增加MSFlexgrid的列数
在第2列后插入一列:

Private Sub Form_Load()
Me.MSHFlexGrid1.Cols = 5
MSHFlexGrid1.Rows = 2
For i = 0 To Me.MSHFlexGrid1.Cols - 1
Me.MSHFlexGrid1.TextMatrix(0, i) = i
Me.MSHFlexGrid1.TextMatrix(1, i) = i
Next
End Sub

 

Private Sub Command1_Click()
Me.MSHFlexGrid1.Cols = Me.MSHFlexGrid1.Cols + 1
Me.MSHFlexGrid1.ColPosition(5) = 3
End Sub

>> MSFlexGrid中的对齐功能的使用
设置MSFlexGrid1.ColAlignment(index)=n


>>得到MSFlexGrid控件中当前选中的一行
msflexgrid1.rowsel就是当前选中行

>> 如何通过代码调节列宽度
msflexgrid1.colwidth(i)=4000

 

 

>> MsFlexGrid操作函数

''合并列
Public Function MergeCol(GridObj As Object, ByVal StartCol As Long, ByVal EndCol As Long, ByVal

ColValue As String, ByVal CurrentRow As Long) As Boolean
If StartCol > EndCol Or StartCol > GridObj.Cols Or CurrentRow > GridObj.Rows Then
MsgBox "对不起,行列设置错误!", vbOKOnly, App.Title
MergeCol = False
Exit Function
End If

For I = StartCol To EndCol
GridObj.MergeCol(I) = True
GridObj.TextArray(faIndex(GridObj, CurrentRow, I)) = ColValue
GridObj.ColAlignment(I) = flexAlignCenterCenter
Next I

GridObj.MergeRow(CurrentRow) = True

MergeCol = True

End Function

''合并行
Public Function MergeRow(GridObj As Object, ByVal StartRow As Long, ByVal EndRow As Long, ByVal

RowValue As String, ByVal CurrentCol As Long) As Boolean
If StartRow > EndRow Or StartRow > GridObj.Rows Or CurrentCol > GridObj.Cols Then
MsgBox "对不起,行列设置错误!", vbOKOnly, App.Title
MergeRow = False
Exit Function
End If

For I = StartRow To EndRow
GridObj.MergeRow(I) = True
GridObj.TextArray(faIndex(GridObj, I, CurrentCol)) = RowValue
GridObj.ColAlignment(CurrentCol) = flexAlignCenterCenter

Next I
GridObj.MergeCol(CurrentCol) = True
MergeRow = True

End Function


增加 MsFlexGrid 的编辑功能

概述
MsFlexGrid 控件没有提供文本编辑的功能,下面的例子演示了如何利用一个TextBox 实现编辑当前网格的功能。

在按下一个键后, 就把TextBox 移动到当前的位置, 并激活。 在键入回车或移动到其他网格时, 就把TextBox 中的内容放到网格中。

实现步骤
1 打开 VB5, 开启一个新的工程。

2 在菜单“工程” 中选择 “部件”, 在列表中选中 “Microsoft FlexGrid Control ..”

3 放一个 MsFlexGrid 控件和一个TextBox 控件(Text1)到 Form1。 修改MsFlexGrid 控件的名称为 Grid1, 设置Grid1 的行,列 为 4, 固定行,列为 0。 设置 Text1 的 Visiable 为 False, BorderStyle 为 None(0)。

4 在Form1 的代码中增加声明:

Const ASC_ENTER = 13 '回车
Dim gRow As Integer
Dim gCol As Integer

5 增加代码到 Grid_KeyPress 过程:

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

6 增加代码到 Text1_KeyPress 过程:

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

7 增加代码到 Text1_LostFocus 过程:

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

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值