MSFlexGrid控件的几种简单的使用方法.docx_第1页
MSFlexGrid控件的几种简单的使用方法.docx_第2页
MSFlexGrid控件的几种简单的使用方法.docx_第3页
MSFlexGrid控件的几种简单的使用方法.docx_第4页
MSFlexGrid控件的几种简单的使用方法.docx_第5页
已阅读5页,还剩8页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

【MSFlexGrid控件的几种简单的使用方法】【日期】VB MSFlexGrid控件的几种简单的使用方法2009-01-10 17:05MSFlexGrid控件的几种简单的使用方法 MSFlexGrid赋值的方法 MSFlexGrid1.TextMatrix(0, 1) = 老师 MSFlexGrid1.TextMatrix(1, 2) = 学生 MSFlexGrid1.TextMatrix(2, 3) = 校长 MSFlexGrid1.TextMatrix(3, 4) = 班主任 MSFlexGrid1.TextMatrix(4, 5) = 教务主任 MSFlexGrid1.AddItem 2 MSFlexGrid1.AddItem 4 MSFlexGrid1.AddItem 6 MSFlexGrid1.AddItem 8MSFLEXGRID 中如何取的某个单元格的数据Private Sub MSFlexGrid1_Click() MsgBox MSFlexGrid1.TextMatrix(MSFlexGrid1.Row, MSFlexGrid1.Col), vbOKOnly, 提示消息 End Sub MSFlexGrid1.Row,表格中的当前行 MSFlexGrid1.Col,表格中的当前列初始化MSFlexGrid Dim i As Integer MSFlexGrid1.Rows = 0 For i = 0 To 10 MSFlexGrid1.AddItem AA + Str(i) For i = 0 To 10 MSFlexGrid1.Col = 1 MSFlexGrid1.Row = i MSFlexGrid1.Text = CStr(i) MSFlexGrid1.Col = 2 MSFlexGrid1.Row = i MSFlexGrid1.Text = CStr(i) & CStr(i) MSFlexGrid1.Col = 3 MSFlexGrid1.Row = i MSFlexGrid1.Text = CStr(i) & CStr(i) & CStr(i) Next i实现MSFlexGrid控件,单行背景为浅灰,双行为兰色 Dim j As Integer With MSFlexGrid1 .AllowBigSelection = True 设置网格样式 .FillStyle = flexFillRepeat For j = 0 To .Rows - 1 .Row = j: .Col = .FixedCols .ColSel = .Cols() - .FixedCols - 1 If j Mod 2 = 0 Then .CellBackColor = &HC0C0C0 浅灰 Else .CellBackColor = vbBlue 兰色 End If Next j End With在MsFlexGrid控件单元格中插入背景图形 Set MSFlexGrid1.CellPicture = LoadPicture(f:tempsnow.bmp)MSFlexGrid控件如何移到最后一行 MSFlexGrid1.TopRow = MSFlexGrid1.Rows - 1隐藏第一行 Private Sub Command1_Click1() MSFlexGrid1.RowHeight(1) = 0 End Sub 隐藏第一列 Private Sub Command_Click2() MSFlexGrid1.ColWidth(1) = 0 End Sub在MSFlexGrid上点击右键,选择属性,选择选择模式,自己任选 点击得到行代码 msgrid1.Col就是你选择的列号 msgrid1.Row 就是你选择的行号 msgrid1.text就是你选择单元格的内容。 将文本赋值给MsFlexGrid的单元格 MsFlexGrid.TextMatrix(3,1)=”Hello” 在MsFlexGrid控件单元格中插入背景图形 Set MsFlexGrid.CellPicture=LoadPicture(“C:temp1.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 = &H1Public 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 FunctionPublic 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 SubPrivate 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-OutDataToText将MsFlexGrid控件中显示的内容输出到文本文件Public Sub OutDataToText(Flex As MSFlexGrid)Dim s As StringDim i As IntegerDim j As IntegerDim k As IntegerDim strTemp As StringOn Error GoTo ErtMe.MousePointer = 11On Error Resume NextDoEventsDim FileNum As IntegerFileNum = FreeFileOpen d:aa.txt For Output As #FileNumWith Flexk = .RowsFor i = 0 To k - 1strTemp = For j = 0 To .Cols - 1DoEventsstrTemp = strTemp & .TextMatrix(i, j) & ,Next jPrint #FileNum, Left(strTemp, Len(strTemp) - 1)Next iEnd WithClose #FileNumMe.MousePointer = 0MsgBox 导出成功Ert:MsgBox Err.DescriptionMe.MousePointer = 0End Sub增加 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 IntegerDim gCol 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 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 box: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, see Text_LostFocus.KeyAscii = 0 Ignore this KeyPress.End IfEnd Sub7 增加代码到 Text1_LostFocus 过程:Private Sub Text1_LostFocus()Dim tmpRow As IntegerDim 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.RowtmpCol = Grid1.Col Set Row and Col back to what they were before Text1_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 Sub8 好了。 按 F5 开始测试。 您可以自由地在 Grid 中移动, 按回车可以开始或结束编辑。使用MsFlexGrid控件的几个函数作者:中国论坛网收集 来源: 加入时间:2004-8-25在VB处理数据显示的时候,使用表格是一种好的方法,虽然DataGrid可以与数据源绑定,但是总有美中不足,就是外观不好看,所以有时应用MsFlexGrid显示数据还是一种比较好的方法,以下几个函数是用来控制MsFlexGrid的程序(本人语言表达能力有限,还请见谅)MsFlexGrid操作函数合并列Public Function MergeCol(GridObj As Object, ByVal StartCol As Long, ByVal EndCol As Long, ByValColValue As String, ByVal CurrentRow As Long) As BooleanIf StartCol EndCol Or StartCol GridObj.Cols Or CurrentRow GridObj.Rows ThenMsgBox 对不起,行列设置错误!, vbOKOnly, App.TitleMergeCol = 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, ByVal StartRow As Long, ByVal EndRow As Long, ByValRowValue As String, ByVal CurrentCol As Long) As BooleanIf StartRow EndRow Or StartRow GridObj.Rows Or CurrentCol GridObj.Cols ThenMsgBox 对不起,行列设置错误!, vbOKOnly, App.TitleMergeRow = FalseExit FunctionEnd IfFor I = StartRow To EndRowGridObj.MergeRow(I) = TrueGridObj.TextArray(faIndex(GridObj, I, CurrentCol) = RowValueGridObj.ColAlignment(CurrentCol) = flexAlignCenterCenterNext IGridObj.MergeCol(CurrentCol) = TrueMergeRow = TrueEnd Function转换索引Public Function faIndex(GridObj As Object, ByVal row As Integer, ByVal col As Integer) As LongIf row GridObj.Rows Or row GridObj.Cols Or col GridObj.Rows Or row GridObj.Cols Or col GridObj.Rows Or row GridObj.Cols Or col 0 ThenMsgBox 对不起,行列设置错误!, vbOKOnly, App.TitleGetItem = Exit FunctionEnd IfGetItem = GridObj.TextArray(faIndex(GridObj, row, col)End Function在msflexgrid控件中每一个cell格的内容是不可以由用户直接编辑的但是我们可以通过一些小技巧来方便的实现这编辑功能来扩展msflexgrid的应用(在实际应用中这是很常用的功能)。你只需按下面的做即可轻松实现编辑msflexgrid控件数据的功能例在窗体上放一文本框txtvalue,和一msflexgrid控件grid文本框控件的keypress事件private sub txtvalue_keypress(keyascii as integer)放入一些处理过程,如只需输入数字时的处理dim ii=1end subprivate sub txtvalue_change()grid.text = txtvalue.textend sub在grid的entercell事件中加入下例代码private sub grid_entercell()txtvalue.text = grid.texttxtvalue.selstart = 0txtvalue.sellength = len(txtvalue.text)end sub当用户输入数据时直接调用文本框的keypress事件private sub grid_keypress(keyascii as integer)txtvalue_keypress keyasciiend subok,这样一个可编辑的msflexgrid控件就完成了,简单吧!Private Sub fgrid1_click() 单击MSFlexGrid sphy = Trim(FGrid1.TextMatrix(FGrid1.Row, 0) delrecord End Sub Private Sub fgrid1_RowColChange()用上下键在MSFlexGrid上选择 sphy = Trim(FGrid1.TextMatrix(FGrid1.Row, 0) delrecord End Sub Private Sub DeleteRecord()删除 Dim I As Integer Dim Ssql As String Dim response As String response = MsgBox(真的删除当前记录?, vbInformation + vbYesNo, 警告) If response = vbYes Then Ssql = delete from login where loginname= & sphy & gcnLeaguers.Execute (Ssql) MsgBox 删除成功!, vbInformation + vbOKOnly, 系统提示 End If Exit Sub End Sub 删除后可能还要刷新一下,你试试吧MSHFlexGrid1.SelectionMode = flexSelectionByRow 还是 Private Sub Form_Load() With MSHFlexGrid1 .FixedCols = 0 .Cols = 2 .Rows = 0 .AddItem 1 & vbTab & 11 .AddItem 2 & vbTab & 22 .AddItem 3 & vbTab & 33 End With End Sub Private Sub MSHFlexGrid1_RowColChange() Static intPrevRow As Long If MSHFlexGrid1.Row intPrevRow Then intPrevRow = MSHFlexGrid1.Row Dim i As Long

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论