VB 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 8
``MSFLEXGRID 中如何取的某個單元格的資料
Private Sub MSFlexGrid1_Click()
MsgBox MSFlexGrid1.TextMatrix(MSFlexGrid1.Row, MSFlexGrid1.Col), vbOKOnly, "提示訊息"
End Sub
MSFlexGrid1.Row,表格中的當前行
MSFlexGrid1.Col,表格中的當前列
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
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
Set MSFlexGrid1.CellPicture = LoadPicture("f:\temp\snow.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:\temp\1.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 = &H1
Public 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 Function
Public 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 Sub
Private 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 String
Dim i As Integer
Dim j As Integer
Dim k As Integer
Dim strTemp As String
On Error GoTo Ert
Me.MousePointer = 11
On Error Resume Next
DoEvents
Dim FileNum As Integer
FileNum = FreeFile
Open "d:aa.txt" For Output As #FileNum
With Flex
k = .Rows
For i = 0 To k - 1
strTemp = ""
For j = 0 To .Cols - 1
DoEvents
strTemp = strTemp & .TextMatrix(i, j) & ","
Next j
Print #FileNum, Left(strTemp, Len(strTemp) - 1)
Next i
End With
Close #FileNum
Me.MousePointer = 0
MsgBox "匯出成功"
Ert:
MsgBox Err.Description
Me.MousePointer = 0
End Sub
增加 MsFlexGrid 的編輯功能
概述
MsFlexGrid 控制元件沒有提供文字編輯的功能,下面的例子演示瞭如何利用一個TextBox 實現編輯當前網格的功能。
在按下一個鍵後, 就把TextBox 移動到當前的位置, 並激活。 在鍵入回車或移動到其他網格時,
就把TextBox 中的內容放到網格中。
實現步驟
1 開啟 VB6, 開啟一個新的工程。
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 Integer
Dim gCol As Integer
5 增加程式碼到 Grid_KeyPress 過程:
Private Sub Grid1_KeyPress(KeyAscii As Integer)
' Move the text box to the current grid cell:
Text1.Top = Grid1.CellTop + Grid1.Top
Text1.Left = Grid1.CellLeft + Grid1.Left
' Save the position of the grids Row and Col for later:
gRow = Grid1.Row
gCol = Grid1.Col
' Make text box same size as current grid cell:
Text1.Width = Grid1.CellWidth - 2 * Screen.TwipsPerPixelX
Text1.Height = Grid1.CellHeight - 2 * Screen.TwipsPerPixelY
' Transfer the grid cell text:
Text1.Text = Grid1.Text
' Show the text box:
Text1.Visible = True
Text1.ZOrder 0 ' 把 Text1 放到最前面!
Text1.SetFocus
' Redirect this KeyPress event to the text box:
If KeyAscii <> ASC_ENTER Then
SendKeys Chr$(KeyAscii)
End If
End Sub
6 增加程式碼到 Text1_KeyPress 過程:
Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii = ASC_ENTER Then
Grid1.SetFocus ' Set focus back to grid, see Text_LostFocus.
KeyAscii = 0 ' Ignore this KeyPress.
End If
End Sub
7 增加程式碼到 Text1_LostFocus 過程:
Private Sub Text1_LostFocus()
Dim tmpRow As Integer
Dim 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.Row
tmpCol = Grid1.Col
' Set Row and Col back to what they were before Text1_LostFocus:
Grid1.Row = gRow
Grid1.Col = gCol
Grid1.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 = tmpRow
Grid1.Col = tmpCol
End Sub
8 好了。 按 F5 開始測試。 您可以自由地在 Grid 中移動, 按回車可以開始或結束編輯。
使用MsFlexGrid控制元件的幾個函式
在VB處理資料顯示的時候,使用表格是一種好的方法,雖然DataGrid可以與資料來源繫結,但是總有美中不足,就是
外觀不好看,所以有時應用MsFlexGrid顯示資料還是一種比較好的方法,以下幾個函式是用來控制MsFlexGrid的程式
(本人語言表達能力有限,還請見諒)
''MsFlexGrid操作函式
''合併列
Public Function MergeCol(GridObj As Object, ByVal StartCol As Long, ByVal EndCol As Long, ByVal
ColValue As String, ByVal CurrentRow As Long) As Boolean
If StartCol > EndCol Or StartCol > GridObj.Cols Or CurrentRow > GridObj.Rows Then
MsgBox "對不起,行列設定錯誤!", vbOKOnly, App.Title
MergeCol = False
Exit Function
End If
For I = StartCol To EndCol
GridObj.MergeCol(I) = True
GridObj.TextArray(faIndex(GridObj, CurrentRow, I)) = ColValue
GridObj.ColAlignment(I) = flexAlignCenterCenter
Next I
GridObj.MergeRow(CurrentRow) = True
MergeCol = True
End Function
''合併行
Public Function MergeRow(GridObj As Object, ByVal StartRow As Long, ByVal EndRow As Long, ByVal
RowValue As String, ByVal CurrentCol As Long) As Boolean
If StartRow > EndRow Or StartRow > GridObj.Rows Or CurrentCol > GridObj.Cols Then
MsgBox "對不起,行列設定錯誤!", vbOKOnly, App.Title
MergeRow = False
Exit Function
End If
For I = StartRow To EndRow
GridObj.MergeRow(I) = True
GridObj.TextArray(faIndex(GridObj, I, CurrentCol)) = RowValue
GridObj.ColAlignment(CurrentCol) = flexAlignCenterCenter
Next I
GridObj.MergeCol(CurrentCol) = True
MergeRow = True
End Function
''轉換索引
Public Function faIndex(GridObj As Object, ByVal row As Integer, ByVal col As Integer) As Long
If row > GridObj.Rows Or row < 0 Or col > GridObj.Cols Or col < 0 Then
MsgBox "對不起,行列設定錯誤!", vbOKOnly, App.Title
faIndex = -1
Exit Function
End If
faIndex = row * GridObj.Cols + col
End Function
''插入行
Public Function SetItem(GridObj As Object, ByVal row As Integer, ByVal col As Integer, ByVal
SetValue As String) As Boolean
If row > GridObj.Rows Or row < 0 Or col > GridObj.Cols Or col < 0 Then
MsgBox "對不起,行列設定錯誤!", vbOKOnly, App.Title
SetItem = False
Exit Function
End If
GridObj.TextArray(faIndex(GridObj, row, col)) = SetValue
SetItem = True
End Function
''得到單元格值
Public Function GetItem(GridObj As Object, ByVal row As Integer, ByVal col As Integer) As String
If row > GridObj.Rows Or row < 0 Or col > GridObj.Cols Or col < 0 Then
MsgBox "對不起,行列設定錯誤!", vbOKOnly, App.Title
GetItem = ""
Exit Function
End If
GetItem = GridObj.TextArray(faIndex(GridObj, row, col))
End Function
在msflexgrid控制元件中每一個cell格的內容是不可以由使用者直接編輯的但是我們可以通過一些小技
巧來方便的實現這編輯功能來擴充套件msflexgrid的應用(在實際應用中這是很常用的功能)。
你只需按下面的做即可輕鬆實現編輯msflexgrid控制元件資料的功能
例在窗體上放一文字框txtvalue,和一msflexgrid控制元件grid
‘文字框控制元件的keypress事件
private sub txtvalue_keypress(keyascii as integer)
‘放入一些處理過程,如只需輸入數字時的處理
dim i
i=1
end sub
private sub txtvalue_change()
grid.text = txtvalue.text
end sub
'在grid的entercell事件中加入下例程式碼
private sub grid_entercell()
txtvalue.text = grid.text
txtvalue.selstart = 0
txtvalue.sellength = len(txtvalue.text)
end sub
'當用戶輸入資料時直接呼叫文字框的keypress事件
private sub grid_keypress(keyascii as integer)
txtvalue_keypress keyascii
end sub
ok,這樣一個可編輯的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
For i = 0 To MSHFlexGrid1.Cols - 1
MsgBox MSHFlexGrid1.TextMatrix(intPrevRow, i)
Next
End If
End Sub
原理
當用戶點選msflexgrid中的某個cell格要輸入資料時,產生entercell事件,在這裡我們對文字
框進行初始化,輸入當前cell格中的內容,並且選中所有文字。當用戶要按下按鍵進行輸入時,就直
接呼叫txtvalue的事件,由文字框來處理.
處理的結果同grid的當前cell同步,使使用者編輯cell格就象使用文字框一樣方便。
網格單元格中文字的多行顯示很簡單,只要把WordWarp屬性改為True就可以了。
1.VB6自帶MSHFlexGrid只支援2048條記錄顯示,這個問題通過裝VB6 的SP6解決.
2.TopRow屬性的使用,這個屬性可以使用程式碼移動MSHFlexGrid的行顯示,如要做一個從上至下的滾動顯示,使用這個屬性解決起來非常方便
3.使用選碼高亮即選中顯示某一行,如要選中第三行,可以使用程式碼:
MSHFlexGrid1.Row = 3
MSHFlexGrid1.TopRow =3
myMSHFlexGrid.ColSel = MSHFlexGrid1.Cols - 1
MSHFlexGrid中單擊某一行變色,如何實現?
Option ExplicitPublic j As Long
Private Sub RowColor(i As Long, j As Long)
'i代表現在點的這一行
Dim n As Long 'n代表列
With Me.MSHFlexGrid1
For n = 1 To .Cols - 1
.Row = i
.Col = n
.CellBackColor = &HC0FFFF
If j > 0 And i <> j Then
.Row = j
.CellBackColor = &HFFFFFF
End If
Next n
j = i
End With
End Sub
Private Sub Form_Load()
With Me.MSHFlexGrid1
.Cols = 5
.Rows = 6
End With
End Sub
Private Sub MSHFlexGrid1_Click()
Call RowColor(Me.MSHFlexGrid1.Row, j)
Debug.Print j
End Sub