机房收费系统中,没少和 MSHFlexGrid控件打交道,一切都是为了优化,下面就和大家分享一下这些神奇的代码:
1、如何保证输出的内容全部居中呢?
这需要从两方面考虑,一是标题行,二是记录行。就拿“查看上机记录”来说吧!代码如下:
上边的2+3+4区“三剑合璧”就能实现文字居中的作用,但是运行的时候发现上边的1区并没有对标题行文字起到居中作用,如下图所示:故得出结论,以上“三剑合璧”法不适用于标题行,因为标题行是固定行,看其颜色就能知道了,灰灰的~~~
那么怎样才能让标题行文字也居中呢,毕竟,美观很重要,不然,优化就失去了意义,期初我的解决办法是这样的,很简单,将标题行代码(即1区代码)写在Form_Load事件里:
<span style="font-size:18px;"><span style="font-size:18px;"> With MSHFlexGrid <span style="color:#ff0000;">.Rows = 2</span> For i = 0 To MSHFlexGrid.Cols - 1 MSHFlexGrid.ColAlignment = 4 Next i .TextMatrix(0, 0) = "卡号" .TextMatrix(0, 1) = "上机日期" .TextMatrix(0, 2) = "上机时间" .TextMatrix(0, 3) = "下机日期" .TextMatrix(0, 4) = "下机时间" .TextMatrix(0, 5) = "消费金额" .TextMatrix(0, 6) = "余额" .TextMatrix(0, 7) = "备注" <span style="color:#ff0000;"> .Rows = .Rows - 1 </span> End With</span></span>运行时加载界面如下,就是上边标识的红色代码促成的~
貌似实现了标题行居中,可是这样做的弊端在于,如果记录过多,滑动右侧的滚轮时,标题行就无法固定了,因其失去了固定行属性。那么怎样做才能两全齐美呢?答案很简单,都是属性问题,ColAlignment属性只对非固定行有效,所以我就将机房收费系统中涉及的所有代码,只要是ColAlignment,都换成了ColAlignmentFixed,后发现,标题行的确能够实现文本居中了,但是原本能够居中的记录行,竟不能居中了!!!便猜想,是不是因为代码中记录集前边没用Trim呢?
用Trim的原因:当我们向数据库表中存入数据的时候也许会无意中多敲上一个空格,所以,查询出的某些记录也许看上去并没有达到文本居中的效果,而是有一种被网格掩盖的感觉;还有可能是数据库设定字符的原因,数据库中若设定10个字符,而假如字段中只有2个字符时,若在VB控件居中显示,又恰恰MSHFlexgrid的宽度不够时,会只显示中间的空格部分,而首位的字符就无法显示了。于是我将记录行有关的代码前边全加了Trim~后发现并没有用,只好将记录行的代码重新改成了ColAlignment,将标题行相关的代码改成了ColAlignmentFixed,这样就万事大吉了~~~
<span style="font-size:18px;"><span style="font-size:18px;"><span style="white-space:pre"> </span><strong>'控制标题行的代码</strong> With MSHFlexGrid .Rows = 2 .FixedRows = 1 For i = 0 To MSHFlexGrid.Cols - 1 MSHFlexGrid.<span style="color:#ff0000;">ColAlignmentFixed</span> = 4 Next i .TextMatrix(0, 0) = "卡号" .TextMatrix(0, 1) = "上机日期" .TextMatrix(0, 2) = "上机时间" .TextMatrix(0, 3) = "下机日期" .TextMatrix(0, 4) = "下机时间" .TextMatrix(0, 5) = "消费金额" .TextMatrix(0, 6) = "余额" .TextMatrix(0, 7) = "备注" <strong> '控制记录行的代码</strong> Do While Not mrc.EOF Rows = .Rows + 1 For i = 0 To MSHFlexGrid.Cols - 1 MSHFlexGrid.<span style="color:#ff0000;">ColAlignment</span> = 4 Next i .TextMatrix(.Rows - 2, 0) = Trim(mrc.Fields(1)) .TextMatrix(.Rows - 2, 1) = Trim(mrc.Fields(6)) .TextMatrix(.Rows - 2, 2) = Trim(mrc.Fields(7)) .TextMatrix(.Rows - 2, 3) = Trim(mrc.Fields(8)) .TextMatrix(.Rows - 2, 4) = Trim(mrc.Fields(9)) .TextMatrix(.Rows - 2, 5) = Trim(mrc.Fields(11)) .TextMatrix(.Rows - 2, 6) = Trim(mrc.Fields(12)) & "" '为了避免上机时就查询上机记录,报错:无效使用NULL .TextMatrix(.Rows - 2, 7) = Trim(mrc.Fields(13)) '显示正常上下机 mrc.MoveNext Loop AutocolWidth Me, MSHFlexGrid '宽度自动适应文本大小 .Rows = .Rows - 1 End With </span></span>
对此只想说:强大的属性!!!(PS:如果想让窗体加载的时候就有标题行,让用户知道查询的结果都有什么,遵循“为人民服务”的宗旨,上边与标题行相关代码就写在Form_Load事件里吧)也许你会觉得奇怪,上边的查询记录,网格自动适应文本大小,如何实现的呢?
2、如何让网格自动适应文本大小?
(1)在模块中声明:
<span style="font-size:18px;"><span style="font-size:18px;">Public Sub AutocolWidth(Form As Form, Grid As MSHFlexGrid) '同一窗体和控件文字大小 Dim FontSize As Integer FontSize = Form.FontSize Form.FontSize = Grid.Font.Size Dim RowNum As Long, ColNum As Long, ColWidth As Double With Grid '遍历每一列 For ColNum = 0 To .Cols - 1 ColWidth = 0 '遍历每一行,找到最长文本 For RowNum = 0 To .Rows - 1 If Form.TextWidth(.TextMatrix(RowNum, ColNum)) > ColWidth Then ColWidth = Form.TextWidth(.TextMatrix(RowNum, ColNum)) End If Next '在最长文本长度的基础上增加长度150缇 .ColWidth(ColNum) = ColWidth + 150 Next End With Form.FontSize = FontSize End Sub</span></span>(2)控件属性设置如下:
(3)最后在控件相关代码处Loop后敲:AutocolWidth Me, myFlexgrid,就像上边的代码中体现的那样~
3、如何在查询记录显示后将MSHFlexGrid控件中的内容清空呢?
期初我用的是下边的代码,运行时报错:VB实时错误30002网格不能包含此行。
For i = 1 To MSHFlexGrid1.Rows - 1
MSHFlexGrid1.RemoveItem i '点击调试会停留在此行,请教大神解释
Next i
后突发奇想:用了一句MSHFlexGrid1.Clear,运行结果是这样滴!
很是烦恼,怎样才好?偷工减料也是一种智慧:不就是造成一种清空的假象嘛!清空的一瞬让MSHFlexGrid控件只剩一行标题不就行了吗,嘻嘻!
MSHFlexGrid1.Rows = 1
4、如何实现单击鼠标选中行?
我的方法是这样的,拿“删除用户”来说吧!
<span style="font-size:18px;">'设置单击鼠标选中行 Private Sub MSHFlexGrid_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single) If MSHFlexGrid.RowSel = 0 Then MsgBox "您这是打算删除标题行的节奏啊!", vbOKOnly + vbExclamation, "提示" Exit Sub Else x = MSHFlexGrid.Row For j = 0 To MSHFlexGrid.Rows - 1 MSHFlexGrid.Row = j For i = 0 To MSHFlexGrid.Cols - 1 MSHFlexGrid.Col = i '为什么这里是等号 MSHFlexGrid.CellBackColor = vbWhite Next i Next j MSHFlexGrid.Row = x If MSHFlexGrid.CellBackColor = &H80C0FF Then For i_Col = 0 To MSHFlexGrid.Cols - 1 MSHFlexGrid.Col = i_Col '而这里不是? MSHFlexGrid.CellBackColor = vbWhite Next i_Col Else For i_Col = 0 To MSHFlexGrid.Cols - 1 MSHFlexGrid.Col = i_Col MSHFlexGrid.CellBackColor = &H80C0FF Next i_Col End If End If End Sub </span>后和小伙伴交流的时候,知道了还有这样的写法,也能实现单击鼠标选中一整行:
首先定义变量:Private CURRENTROW As Integer
<span style="font-size:18px;">Private Sub MSHFlexGrid1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single) With MSHFlexGrid1 .Row = .MouseRow CURRENTROW = .Row .Col = 0 .ColSel = .Cols - 1 End With End Sub Private Sub MSHFlexGrid1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single) With MSHFlexGrid1 .RowSel = CURRENTROW .ColSel = .Cols - 1 End With End Sub </span>总结到此,疑问总是用的,请大神帮我解答实时错误30002~~~