MSFlexGrid使用细则.docx
《MSFlexGrid使用细则.docx》由会员分享,可在线阅读,更多相关《MSFlexGrid使用细则.docx(12页珍藏版)》请在冰豆网上搜索。
MSFlexGrid使用细则
VB中MsFlexGrid控件的使用细则(收集)
>>将文本赋值给MsFlexGrid的单元格
MsFlexGrid.TextMatrix(3,1)=”Hello”
>>在MsFlexGrid控件单元格中插入背景图形
SetMsFlexGrid.CellPicture=LoadPicture(“C:
\temp\1.bmp”)
>>选中某个单元
MsFlexGrid.Row=1
MsFlexGrid.Col=1
>>用粗体格式化当前选中单元
MsFlexGrid.CellFontBold=True
>>添加新的一行
使用AddItem方法,用Tab字符分开不同单元格的内容
dimrowasstring
row=”AAA”&vbtab&”bbb”
MsFlexFrid1.addItemrow
>>怎样来实现MSFlexGrid控件单数行背景为白色,双数的行背景为蓝色?
DimiAsInteger
WithMSFlexGrid1
.AllowBigSelection=True’设置网格样式
.FillStyle=flexFillRepeat
Fori=0To.Rows-1
.Row=i:
.Col=.FixedCols
.ColSel=.Cols()-.FixedCols-1
IfiMod2=0Then
.CellBackColor=&HC0C0C0’浅灰
Else
.CellBackColor=vbBlue’兰色
EndIf
Nexti
EndWith
>>MSFlexGrid控件如何移到最后一行
MSFlexGrid1.TopRow=MSFlexGrid1.Rows–1
>>如何判断msflexgrid有无滚动条
DeclareFunctionGetScrollRangeLib"user32"(ByValhWndAsLong,ByValnBarAsLong,lpMinPosAs
Long,lpMaxPosAsLong)AsLong
PublicConstSB_HORZ=&H0
PublicConstSB_VERT=&H1
PublicFunctionVsScroll(MshGridAsMSHFlexGrid)AsBoolean’判断水平滚动条的可见性
DimiAsLong
VsScroll=False
i=GetScrollRange(MshGrid.hWnd,SB_HORZ,lpMinPos,lpMaxPos)
IflpMaxPos<>lpMinPosThenVsScroll=True
EndFunction
PublicFunctionHeScroll(MshGridAsMSHFlexGrid)AsBoolean’判断垂直滚动条的可见性
DimiAsLong
HeScroll=False
i=GetScrollRange(MshGrid.hWnd,SB_VERT,lpMinPos,lpMaxPos)
IflpMaxPos<>lpMinPosThenHeScroll=True
EndFunction
>>程序运行时,想动态增加MSFlexgrid的列数
在第2列后插入一列:
PrivateSubForm_Load()
Me.MSHFlexGrid1.Cols=5
MSHFlexGrid1.Rows=2
Fori=0ToMe.MSHFlexGrid1.Cols-1
Me.MSHFlexGrid1.TextMatrix(0,i)=i
Me.MSHFlexGrid1.TextMatrix(1,i)=i
Next
EndSub
PrivateSubCommand1_Click()
Me.MSHFlexGrid1.Cols=Me.MSHFlexGrid1.Cols+1
Me.MSHFlexGrid1.ColPosition(5)=3
EndSub
>>请教MSFlexGrid中的对齐功能的使用
设置MSFlexGrid1.ColAlignment(index)=n
>>得到MSFlexGrid控件中当前选中的一行
msflexgrid1.rowsel就是当前选中行
>>如何通过代码调节列宽度
msflexgrid1.colwidth(i)=4000
将MsFlexGrid控件的内容输出到文本
2004-03-1914:
25:
18
'OutDataToText
'将MsFlexGrid控件中显示的内容输出到文本文件
PublicSubOutDataToText(FlexAsMSFlexGrid)
DimsAsString
DimiAsInteger
DimjAsInteger
DimkAsInteger
DimstrTempAsString
OnErrorGoToErt
Me.MousePointer=11
OnErrorResumeNext
DoEvents
DimFileNumAsInteger
FileNum=FreeFile
Open"d:
aa.txt"ForOutputAs#FileNum
WithFlex
k=.Rows
Fori=0Tok-1
strTemp=""
Forj=0To.Cols-1
DoEvents
strTemp=strTemp&.TextMatrix(i,j)&","
Nextj
Print#FileNum,Left(strTemp,Len(strTemp)-1)
Nexti
EndWith
Close#FileNum
Me.MousePointer=0
MsgBox"导出成功"
Ert:
MsgBoxErr.Description
Me.MousePointer=0
EndSub
增加MsFlexGrid的编辑功能
(作者:
佚名加载日期:
2002/3/31)
概述
MsFlexGrid控件没有提供文本编辑的功能,下面的例子演示了如何利用一个TextBox实现编辑当前网格的功能
。
在按下一个键后,就把TextBox移动到当前的位置,并激活。
在键入回车或移动到其他网格时,
就把TextBox中的内容放到网格中。
实现步骤
1打开VB5,开启一个新的工程。
2在菜单“工程”中选择“部件”,在列表中选中“MicrosoftFlexGridControl..”
3放一个MsFlexGrid控件和一个TextBox控件(Text1)到Form1。
修改MsFlexGrid控件的名称为Grid1,
设置Grid1的行,列为4,固定行,列为0。
设置Text1的Visiable为False,BorderStyle为
None(0)。
4在Form1的代码中增加声明:
ConstASC_ENTER=13'回车
DimgRowAsInteger
DimgColAsInteger
5增加代码到Grid_KeyPress过程:
PrivateSubGrid1_KeyPress(KeyAsciiAsInteger)
'Movethetextboxtothecurrentgridcell:
Text1.Top=Grid1.CellTop+Grid1.Top
Text1.Left=Grid1.CellLeft+Grid1.Left
'SavethepositionofthegridsRowandColforlater:
gRow=Grid1.Row
gCol=Grid1.Col
'Maketextboxsamesizeascurrentgridcell:
Text1.Width=Grid1.CellWidth-2*Screen.TwipsPerPixelX
Text1.Height=Grid1.CellHeight-2*Screen.TwipsPerPixelY
'Transferthegridcelltext:
Text1.Text=Grid1.Text
'Showthetextbox:
Text1.Visible=True
Text1.ZOrder0'把Text1放到最前面!
Text1.SetFocus
'RedirectthisKeyPresseventtothetextbox:
IfKeyAscii<>ASC_ENTERThen
SendKeysChr$(KeyAscii)
EndIf
EndSub
6增加代码到Text1_KeyPress过程:
PrivateSubText1_KeyPress(KeyAsciiAsInteger)
IfKeyAscii=ASC_ENTERThen
Grid1.SetFocus'Setfocusbacktogrid,seeText_LostFocus.
KeyAscii=0'IgnorethisKeyPress.
EndIf
EndSub
7增加代码到Text1_LostFocus过程:
PrivateSubText1_LostFocus()
DimtmpRowAsInteger
DimtmpColAsInteger
'SavecurrentsettingsofGridRowandcol.Thisisneededonlyif
'thefocusissetsomewhereelseintheGrid.
tmpRow=Grid1.Row
tmpCol=Grid1.Col
'SetRowandColbacktowhattheywerebeforeText1_LostFocus:
Grid1.Row=gRow
Grid1.Col=gCol
Grid1.Text=Text1.Text'Transfertextbacktogrid.
Text1.SelStart=0'Returncarettobeginning.
Text1.Visible=False'Disabletextbox.
'ReturnrowandColcontents:
Grid1.Row=tmpRow
Grid1.Col=tmpCol
EndSub
8好了。
按F5开始测试。
您可以自由地在Grid中移动,按回车可以开始或结束编辑。
使用MsFlexGrid控件的几个函数
作者:
中国论坛网收集来源:
加入时间:
2004-8-25
在VB处理数据显示的时候,使用表格是一种好的方法,虽然DataGrid可以与数据源绑定,但是总有美中不足,就是
外观不好看,所以有时应用MsFlexGrid显示数据还是一种比较好的方法,以下几个函数是用来控制MsFlexGrid的
程序
(本人语言表达能力有限,还请见谅)
''MsFlexGrid操作函数
''合并列
PublicFunctionMergeCol(GridObjAsObject,ByValStartColAsLong,ByValEndColAsLong,ByVal
ColValueAsString,ByValCurrentRowAsLong)AsBoolean
IfStartCol>EndColOrStartCol>GridObj.ColsOrCurrentRow>GridObj.RowsThen
MsgBox"对不起,行列设置错误!
",vbOKOnly,App.Title
MergeCol=False
ExitFunction
EndIf
ForI=StartColToEndCol
GridObj.MergeCol(I)=True
GridObj.TextArray(faIndex(GridObj,CurrentRow,I))=ColValue
GridObj.ColAlignment(I)=flexAlignCenterCenter
NextI
GridObj.MergeRow(CurrentRow)=True
MergeCol=True
EndFunction
''合并行
PublicFunctionMergeRow(GridObjAsObject,ByValStartRowAsLong,ByValEndRowAsLong,ByVal
RowValueAsString,ByValCurrentColAsLong)AsBoolean
IfStartRow>EndRowOrStartRow>GridObj.RowsOrCurrentCol>GridObj.ColsThen
MsgBox"对不起,行列设置错误!
",vbOKOnly,App.Title
MergeRow=False
ExitFunction
EndIf
ForI=StartRowToEndRow
GridObj.MergeRow(I)=True
GridObj.TextArray(faIndex(GridObj,I,CurrentCol))=RowValue
GridObj.ColAlignment(CurrentCol)=flexAlignCenterCenter
NextI
GridObj.MergeCol(CurrentCol)=True
MergeRow=True
EndFunction
''转换索引
PublicFunctionfaIndex(GridObjAsObject,ByValrowAsInteger,ByValcolAsInteger)AsLong
Ifrow>GridObj.RowsOrrow<0Orcol>GridObj.ColsOrcol<0Then
MsgBox"对不起,行列设置错误!
",vbOKOnly,App.Title
faIndex=-1
ExitFunction
EndIf
faIndex=row*GridObj.Cols+col
EndFunction
''插入行
PublicFunctionSetItem(GridObjAsObject,ByValrowAsInteger,ByValcolAsInteger,ByVal
SetValueAsString)AsBoolean
Ifrow>GridObj.RowsOrrow<0Orcol>GridObj.ColsOrcol<0Then
MsgBox"对不起,行列设置错误!
",vbOKOnly,App.Title
SetItem=False
ExitFunction
EndIf
GridObj.TextArray(faIndex(GridObj,row,col))=SetValue
SetItem=True
EndFunction
''得到单元格值
PublicFunctionGetItem(GridObjAsObject,ByValrowAsInteger,ByValcolAsInteger)AsString
Ifrow>GridObj.RowsOrrow<0Orcol>GridObj.ColsOrcol<0Then
MsgBox"对不起,行列设置错误!
",vbOKOnly,App.Title
GetItem=""
ExitFunction
EndIf
GetItem=GridObj.TextArray(faIndex(GridObj,row,col))
EndFunction
在msflexgrid控件中每一个cell格的内容是不可以由用户直接编辑的但是我们可以通过一些小技
巧来方便的实现这编辑功能来扩展msflexgrid的应用(在实际应用中这是很常用的功能)。
你只需按下面的做即可轻松实现编辑msflexgrid控件数据的功能
例在窗体上放一文本框txtvalue,和一msflexgrid控件grid
‘文本框控件的keypress事件
privatesubtxtvalue_keypress(keyasciiasinteger)
‘放入一些处理过程,如只需输入数字时的处理
dimi
i=1
endsub
privatesubtxtvalue_change()
grid.text=txtvalue.text
endsub
'在grid的entercell事件中加入下例代码
privatesubgrid_entercell()
txtvalue.text=grid.text
txtvalue.selstart=0
txtvalue.sellength=len(txtvalue.text)
endsub
'当用户输入数据时直接调用文本框的keypress事件
privatesubgrid_keypress(keyasciiasinteger)
txtvalue_keypresskeyascii
endsub
ok,这样一个可编辑的msflexgrid控件就完成了,简单吧!
!
原理
当用户点击msflexgrid中的某个cell格要输入数据时,产生entercell事件,在这里我们对文本
框进行初始化,输入当前cell格中的内容,并且选中所有文本。
当用户要按下按键进行输入时,就直
接调用txtvalue的事件,由文本框来处理.
处理的结果同grid的当前cell同步,使用户编辑cell格就象使用文本框一样方便
'MsFlexGrid操作函数
'合并列
PublicFunctionMergeCol(GridObjAsObject,ByValStartColAsLong,ByValEndColAsLong,ByValColValueAsString,ByValCurrentRowAsLong)AsBoolean
IfStartCol>EndColOrStartCol>GridObj.ColsOrCurrentRow>GridObj.RowsThen
MsgBox"对不起,行列设置错误!
",vbOKOnly,App.Title
MergeCol=False
ExitFunction
EndIf
ForI=StartColToEndCol
GridObj.MergeCol(I)=True
GridObj.TextArray(faIndex(GridObj,CurrentRow,I))=ColValue
GridObj.ColAlignment(I)=flexAlignCenterCenter
NextI
GridObj.MergeRow(CurrentRow)=True
MergeCol=True
EndFunction
'合并行
PublicFunctionMergeRow(GridObjAsObject,ByValStartRowAsLong,ByValEndRowAsLong,ByValRowValueAsString,ByValCurrentColAsLong)AsBoolean
IfStartRow>EndRowOrStartRow>GridObj.RowsOrCurrentCol>GridObj.ColsThen
MsgBox"对不起,行列设置错误!
",vbOKOnly,App.Title
MergeRow=False
ExitFunction
EndIf
ForI=StartRowToEndRow
GridObj.MergeRow(I)=True
GridObj.TextArray(faIndex(GridObj,I,CurrentCol))=RowValue
GridObj.ColAlignment(CurrentCol)=flexAlignCenterCenter
NextI
GridObj.MergeCol(CurrentCol)=True
MergeRow=True
EndFunction
'转换索引
PublicFunctionfaIndex(GridObjAsObject,ByValrowAsInteger,ByValcolAsInteger)AsLong
Ifrow>GridObj.RowsOrrow<0Orcol>GridObj.ColsOrcol<0Then
MsgBox"对不起,行列设置错误!
",vbOKOnly,App.Title
faIndex=-1
ExitFunction
EndIf
faIndex=row*GridObj.Cols+col
EndFunction
'插入行
PublicFunctionSetItem(GridObjAsObject,ByValrowAsInteger,ByValcolAsInteger,ByValSetValueAsString)AsBoolean
Ifrow>GridObj.RowsOrrow<0Orcol>GridObj.ColsOrcol<0Then
MsgBox"对不起,行列设置错误!
",vbOKOnly,App.Title
SetItem=False
ExitFunction
EndIf
GridObj.TextArray(faIndex(GridObj,row,col))=SetValue
SetI