vsFlexGrid常用功能介绍
vsFlexGrid是ComponentOne公司的一个ActiveX控件,它与MS的MSHFlexGrid控件在功能上是兼容的,即MSHFlexGrid所具备的属性和方法,它也都具备,只有少数几个属性名称略有不同,使用MSHFlexGrid编写的程序,只需要作较小的调整就可以转换过来;但相比MSHFlexGrid,vsFlexGrid多了很多功能,它的属性和事件非常丰富,可以实现非常灵活的控制和快捷的编码,下面主要对一些常用和实用的功能进行介绍:
一、数据批量操作
l 利用Cell属性批量存取数据
比如要清除一块区域的文本,或者设置一块区域的字体,不用循环处理,只需要一条语句就可以完成,如:
vsFlexGrid.Cell(flexcpText,1,1,5,5) = “” ‘设置(1,1)-(5,5)这块单元格区域的文本都为空
vsFlexGrid.Cell(flexcpFontBold,1,1,5,5)=True ‘设置(1,1)-(5,5)这块单元格区域的字体都为粗体
类似还可以设置的有单元格对齐、图片、颜色等。
而语句:strText = vsFlexGrid.Cell(flexcpText,1,1,5,5)
返回一个用vbTab间隔列,vbCr间隔行的字符串,当然也可以反过来赋值,这样该区域内的每个单元格都可以赋不同的值。
l 使用-1对所有行列进行处理
RowHeight(-1)=300 ‘将所有行的高度设置为300Twip,可以类似使用的属性有:
RowHidden,ColWidth,ColHidden,ColAlignment等
二、大量数据存放
vsFlexGrid可以存放数据的地方有:CellText,CellData,ColData,RowData
除了CellText是String类型外,其他都是Variant类型,也就是说你可以将任意类型的数据存放在表格中,比如日期(Date),类型变量(Type),集合(Collection),数组,记录集(RecordSet),以及各种对象(Object),这对于关联数据比较多的程序非常有用。
注意:vsFlexGrid.Cell(flexcpText,1,1) = rsData!Name实际是存放了一个ADO.Field对象,应该使用CStr(rsData!Name)对类型进行明确。
三、编辑特性
vsFlexGrid的单元格是可以编辑的,并且有以下几种编辑形式:
l 文本框:只要Editable设置为可编辑,每个单元格缺省就是TextBox编辑样式
l 下拉框:
设置当前单元格的ComboList属性或者指定列的ColComboList属性,即可实现VB.ComboBox样式的单元编辑,而且下拉界面中可以显示多列,如:
vsFlexGrid.ComboList = _
“|#100*1;张三” & vbTab & “男” & vbTab & “28岁” & _
“|#101;李四” & vbTab & “男” & vbTab & “29岁” & _
“|#102;王五” & vbTab & “男” & vbTab & “30岁”
其中”|”间隔的是各行数据,vbTab间隔的是各列数据;第1个”|”表示下拉框可以输入,”#”后面的是每行的ItemData, 第一个ItemData后面”*1”表示是第1列的数据在选择后显示在ComboBox中。
l 复选框:
设置指定列的ColDataType = flexDTBoolean,或者指定当前元单格的CellChecked属性即可。
l 选择按钮:
设置当前单元格的ComboList属性或者指定列的ColComboList属性为”…”即可,主要配合CellButtonClick事件使用。
四、界面特性
1) 边框
除了表格线之外,vsFlexGrid还可以类似Excel一样,设置单元格式的边框线条,包括颜色、粗细,有无:
VSFlexGrid.CellBorder Color As OLE_COLOR, Left As Integer, Top As Integer, Right As Integer, Bottom As Integer, Vertical As Integer, Horizontal As Integer
2) 自动行高,列宽
使用AutoSize方法,可以使列宽、或者行高根据单元格的内容进行自适应调整。
其中列宽也可以通过双击表头进行自适应调整。
3) 树形缩进(TreeList)
通过设置表格行的IsSubtotal、RowOutlineLevel属性,可以实现树形风格的缩进层次样式,这样vsFlexGrid就可以实现TreeView和ListView的结合。
4) 自绘
通过设置OwnerDraw属性,可对实现对单元格内容的自绘处理,主要结合DrawCell事件进行:
DrawCell(ByVal hDC As Long, ByVal Row As Long, ByVal col As Long, ByVal left As Long, ByVal top As Long, ByVal right As Long, ByVal bottom As Long, done As Boolean)
5) 背景
可以为vsFlexGrid设置背景图,背景图相对单元格式内容是透明的,并且可以设置对齐和平铺属性。
6) 列排序,行、列拖动
设置vsFlexGrid.ExplorerBar属性,就可以实现类似Windows的资源管理器一样,点击列头进行列数据排序,或者用鼠标拖动改变列、行的顺序。
7) 冻结行列
除了固定行列头外,还可以类似Excel一样,通过vsFlexGrid.FrozenCols/FrozenRows设置冻结行列,冻结行列在滚动时,和固定行列是一样的效果,但是光标可以进入到冻结行列范围内的单元格中去。
五、丰富的事件控制:
vsFlexGrid一共有50多个事件,这些事件主要用于编辑和界面控制,在使用过程中,经常可能会发现某个事件特别有用;因此充分利用这些事件,可以很方便地实现很多功能,下面是它支持的事件及对应列表,粗体是较常用的,从定义基本就可以看出它的作用;详细的事件说明,可以查看帮助。
l AfterCollapse(ByVal Row As Long, ByVal State As Integer)
l AfterDataRefresh()
l AfterEdit(ByVal Row As Long, ByVal Col As Long)
l AfterMoveColumn(ByVal Col As Long, Position As Long)
l AfterMoveRow(ByVal Row As Long, Position As Long)
l AfterRowColChange(ByVal OldRow As Long, ByVal OldCol As Long, ByVal NewRow As Long, ByVal NewCol As Long)
l AfterScroll(ByVal OldTopRow As Long, ByVal OldLeftCol As Long, ByVal NewTopRow As Long, ByVal NewLeftCol As Long)
l AfterSelChange(ByVal OldRowSel As Long, ByVal OldColSel As Long, ByVal NewRowSel As Long, ByVal NewColSel As Long)
l AfterSort(ByVal Col As Long, Order As Integer)
l AfterUserFreeze()
l AfterUserResize(ByVal Row As Long, ByVal Col As Long)
l BeforeCollapse(ByVal Row As Long, ByVal State As Integer, Cancel As Boolean)
l BeforeDataRefresh(Cancel As Boolean)
l BeforeEdit(ByVal Row As Long, ByVal Col As Long, Cancel As Boolean)
l BeforeMouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single, Cancel As Boolean)
l BeforeMoveColumn(ByVal Col As Long, Position As Long)
l BeforeMoveRow(ByVal Row As Long, Position As Long)
l BeforePageBreak(ByVal Row As Long, BreakOK As Boolean)
l BeforeRowColChange(ByVal OldRow As Long, ByVal OldCol As Long, ByVal NewRow As Long, ByVal NewCol As Long, Cancel As Boolean)
l BeforeScroll(ByVal OldTopRow As Long, ByVal OldLeftCol As Long, ByVal NewTopRow As Long, ByVal NewLeftCol As Long, Cancel As Boolean)
l BeforeScrollTip(ByVal Row As Long)
l BeforeSelChange(ByVal OldRowSel As Long, ByVal OldColSel As Long, ByVal NewRowSel As Long, ByVal NewColSel As Long, Cancel As Boolean)
l BeforeSort(ByVal Col As Long, Order As Integer)
l BeforeUserResize(ByVal Row As Long, ByVal Col As Long, Cancel As Boolean)
l CellButtonClick(ByVal Row As Long, ByVal Col As Long)
l CellChanged(ByVal Row As Long, ByVal Col As Long)
l ChangeEdit()
l Click()
l ComboCloseUp(ByVal Row As Long, ByVal Col As Long, FinishEdit As Boolean)
l ComboDropDown(ByVal Row As Long, ByVal Col As Long)
l Compare(ByVal Row1 As Long, ByVal Row2 As Long, Cmp As Integer)
l DblClick()
l DragDrop(Source As Control, X As Single, Y As Single)
l DragOver(Source As Control, X As Single, Y As Single, State As Integer)
l DrawCell(ByVal hDC As Long, ByVal Row As Long, ByVal Col As Long, ByVal Left As Long, ByVal Top As Long, ByVal Right As Long, ByVal Bottom As Long, Done As Boolean)
l EndAutoSearch()
l EnterCell()
l Error(ByVal ErrorCode As Long, ShowMsgBox As Boolean)
l FilterData(ByVal Row As Long, ByVal Col As Long, Value As String, ByVal SavingToDB As Boolean, WantThisCol As Boolean)
l GetHeaderRow(ByVal Row As Long, HeaderRow As Long)
l GotFocus()
l KeyDown(KeyCode As Integer, Shift As Integer)
l KeyDownEdit(ByVal Row As Long, ByVal Col As Long, KeyCode As Integer, ByVal Shift As Integer)
l KeyPress(KeyAscii As Integer)
l KeyPressEdit(ByVal Row As Long, ByVal Col As Long, KeyAscii As Integer)
l KeyUp(KeyCode As Integer, Shift As Integer)
l KeyUpEdit(ByVal Row As Long, ByVal Col As Long, KeyCode As Integer, ByVal Shift As Integer)
l LeaveCell()
l LostFocus()
l MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
l MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
l MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
l OLECompleteDrag(Effect As Long)
l OLEDragDrop(Data As VSFlex8Ctl.VSDataObject, Effect As Long, ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
l OLEDragOver(Data As VSFlex8Ctl.VSDataObject, Effect As Long, ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single, State As Integer)
l OLEGiveFeedback(Effect As Long, DefaultCursors As Boolean)
l OLESetCustomDataObject(CustomDataObject As Variant)
l OLESetData(Data As VSFlex8Ctl.VSDataObject, DataFormat As Integer)
l OLEStartDrag(Data As VSFlex8Ctl.VSDataObject, AllowedEffects As Long)
l RowColChange()
l SelChange()
l SetupEditStyle(ByVal Row As Long, ByVal Col As Long, ByVal IsCombo As Boolean, Style As Long, StyleEx As Long)
l SetupEditWindow(ByVal Row As Long, ByVal Col As Long, ByVal EditWindow As Long, ByVal IsCombo As Boolean)
l StartAutoSearch()
l StartEdit(ByVal Row As Long, ByVal Col As Long, Cancel As Boolean)
l StartPage(ByVal hDC As Long, ByVal Page As Long, Cancel As Boolean)
l Validate(Cancel As Boolean)
l ValidateEdit(ByVal Row As Long, ByVal Col As Long, Cancel As Boolean)
六、附加功能:
1) 属性页:
在vsFlexGrid的属性页中,可以对表格进行格式和内容设计,这样可以省去一些代码(这部分属性页处理的内容保存在窗体的frx文件中)。
2) 导入/导出:
通过LoadGrid/SaveGrid方法,可以对表格内容进行导入导出;导入导出的格式有几种:
l 它自已的二进制格式。
l 带一定分隔符的文本文件。
l Excel97格式(不需要安装 Excel,速度很快,但有一些属性不支持)。
3) 打印
通过PrintGrid方法,可以将vsFlexGrid按照当前的格式和数据进行打印输出,但这个功能比较简单,比如不能进行预览,设置页眉页脚等。(ComponentOne的另一套控件vsView与vsFlexGrid配合可以实现强大的打印相关功能)
作为一个只有600多K大小的控件(vsFlexGrid.ocx),功能这么强大确实非常不错,在ZLHIS中也进行了大量的使用,上面只是简略列举了它的部分功能,更详细的功能可以运行它的Demo进行展示,也可以查看它的帮助文件,都比较详尽