《VB中MsFlexGrid控件的使用细则.pdf》由会员分享,可在线阅读,更多相关《VB中MsFlexGrid控件的使用细则.pdf(9页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、VB 中 MsFlexGrid 控件的使用细则>>在 MsFlexGrid 控件单元格中插入背景图形Set MsFlexGrid.CellPicture=LoadPicture(“C:/temp/1.bmp”)>>选中某个单元MsFlexGrid.Row=1MsFlexGrid.Col=1>>用粗体格式化当前选中单元MsFlexGrid.CellFontBold=True>>添加新的一行使用 AddItem 方法,用 Tab 字符分开不同单元格的内容dim row as stringrow=”AAA”&vbtab&”bbb”MsF
2、lexFrid1.addItem row>>怎样来实现 MSFlexGrid 控件单数行背景为白色,双数的行背景为蓝色?Dim i As IntegerWith MSFlexGrid1.AllowBigSelection=True 设置网格样式.FillStyle=flexFillRepeatFor i=0 To.Rows-1.Row=i:.Col=.FixedCols.ColSel=.Cols()-.FixedCols-1If i Mod 2=0 Then.CellBackColor=&HC0C0C0浅灰Else.CellBackColor=vbBlue 兰色End If
3、Next iEnd With>>MSFlexGrid 控件如何移到最后一行MSFlexGrid1.TopRow=MSFlexGrid1.Rows 1>>如何判断 msflexgrid 有无滚动条Declare Function GetScrollRange Lib user32(ByValhWnd As Long,ByVal nBar As Long,lpMinPos As Long,lpMaxPos As Long)As LongPublic Const SB_HORZ=&H0Public Const SB_VERT=&H1Public Functio
4、n VsScroll(MshGrid As MSHFlexGrid)AsBoolean判断水平滚动条的可见性Dim i As LongVsScroll=Falsei=GetScrollRange(MshGrid.hWnd,SB_HORZ,lpMinPos,lpMaxPos)If lpMaxPos<>lpMinPos Then VsScroll=TrueEnd FunctionPublic Function HeScroll(MshGrid As MSHFlexGrid)AsBoolean判断垂直滚动条的可见性Dim i As LongHeScroll=Falsei=GetScrol
5、lRange(MshGrid.hWnd,SB_VERT,lpMinPos,lpMaxPos)If lpMaxPos<>lpMinPos Then HeScroll=TrueEnd Function>>程序运行时,想动态增加 MSFlexgrid 的列数在第 2 列后插入一列:Private Sub Form_Load()Me.MSHFlexGrid1.Cols=5MSHFlexGrid1.Rows=2For i=0 To Me.MSHFlexGrid1.Cols-1Me.MSHFlexGrid1.TextMatrix(0,i)=iMe.MSHFlexGrid1.Text
6、Matrix(1,i)=iNextEnd SubPrivate Sub Command1_Click()Me.MSHFlexGrid1.Cols=Me.MSHFlexGrid1.Cols+1Me.MSHFlexGrid1.ColPosition(5)=3End Sub>>MSFlexGrid 中的对齐功能的使用设置 MSFlexGrid1.ColAlignment(index)=n>>得到 MSFlexGrid 控件中当前选中的一行msflexgrid1.rowsel 就是当前选中行>>如何通过代码调节列宽度msflexgrid1.colwidth(i)=4
7、000>>MsFlexGrid 操作函数合并列Public Function MergeCol(GridObj As Object,ByValStartCol As Long,ByVal EndCol As Long,ByValColValueAs String,ByVal CurrentRow As Long)As BooleanIf StartCol>EndCol Or StartCol>GridObj.Cols OrCurrentRow>GridObj.Rows ThenMsgBox 对不起,行列设置错误!,vbOKOnly,App.TitleMergeCo
8、l=FalseExit FunctionEnd IfFor I=StartCol To EndColGridObj.MergeCol(I)=TrueGridObj.TextArray(faIndex(GridObj,CurrentRow,I)=ColValueGridObj.ColAlignment(I)=flexAlignCenterCenterNext IGridObj.MergeRow(CurrentRow)=TrueMergeCol=TrueEnd Function合并行Public Function MergeRow(GridObj As Object,ByValStartRow A
9、s Long,ByVal EndRow As Long,ByValRowValue As String,ByVal CurrentCol As Long)AsBooleanIf StartRow>EndRow Or StartRow>GridObj.RowsOr CurrentCol>GridObj.Cols ThenMsgBox 对不起,行列设置错误!,vbOKOnly,App.TitleMergeRow=FalseExit FunctionEnd IfFor I=StartRow To EndRowGridObj.MergeRow(I)=TrueGridObj.TextA
10、rray(faIndex(GridObj,I,CurrentCol)=RowValueGridObj.ColAlignment(CurrentCol)=flexAlignCenterCenterNext IGridObj.MergeCol(CurrentCol)=TrueMergeRow=TrueEnd Function增加 MsFlexGrid 的编辑功能概述MsFlexGrid 控件没有提供文本编辑的功能,下面的例子演示了如何利用一个 TextBox 实现编辑当前网格的功能。在按下一个键后,就把 TextBox 移动到当前的位置,并激活。在键入回车或移动到其他网格时,就把 TextBox
11、中的内容放到网格中。实现步骤1 打开 VB5,开启一个新的工程。2 在菜单“工程”中选择“部件”,在列表中选中“MicrosoftFlexGrid 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 IntegerDim gCol
12、As Integer5 增加代码到 Grid_KeyPress 过程:Private Sub Grid1_KeyPress(KeyAscii As Integer)Move the text box to the current grid cell:Text1.Top=Grid1.CellTop+Grid1.TopText1.Left=Grid1.CellLeft+Grid1.Left Save the position of the grids Row and Col for later:gRow=Grid1.RowgCol=Grid1.Col Make text box same size
13、 as current grid cell:Text1.Width=Grid1.CellWidth-2*Screen.TwipsPerPixelXText1.Height=Grid1.CellHeight-2*Screen.TwipsPerPixelY Transfer the grid cell text:Text1.Text=Grid1.Text Show the text box:Text1.Visible=TrueText1.ZOrder 0 把 Text1 放到最前面!Text1.SetFocus Redirect this KeyPress event to the text bo
14、x:If KeyAscii<>ASC_ENTER ThenSendKeys Chr$(KeyAscii)End IfEnd Sub6 增加代码到 Text1_KeyPress 过程:Private Sub Text1_KeyPress(KeyAscii As Integer)If KeyAscii=ASC_ENTER ThenGrid1.SetFocus Set focus back to grid,seeText_LostFocus.KeyAscii=0 Ignore this KeyPress.End IfEnd Sub7 增加代码到 Text1_LostFocus 过程:Pr
15、ivate Sub Text1_LostFocus()Dim tmpRow As IntegerDim tmpCol As Integer Save current settings of Grid Row and col.This is neededonly if the focus is set somewhere else in the Grid.tmpRow=Grid1.RowtmpCol=Grid1.Col Set Row and Col back to what they were beforeText1_LostFocus:Grid1.Row=gRowGrid1.Col=gColGrid1.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=tmpRowGrid1.Col=tmpColEnd Sub