2 回答

TA貢獻(xiàn)1856條經(jīng)驗(yàn) 獲得超11個(gè)贊
'MSFlexGrid賦值的方法 MSFlexGrid1.TextMatrix(0, 1) = "老師"
MSFlexGrid1.TextMatrix(1, 2) = "學(xué)生"
MSFlexGrid1.TextMatrix(2, 3) = "校長(zhǎng)"
MSFlexGrid1.TextMatrix(3, 4) = "班主任"
MSFlexGrid1.TextMatrix(4, 5) = "教務(wù)主任"
MSFlexGrid1.AddItem 2
MSFlexGrid1.AddItem 4
MSFlexGrid1.AddItem 6
MSFlexGrid1.AddItem 8
``MSFLEXGRID 中如何取的某個(gè)單元格的數(shù)據(jù)
Private Sub MSFlexGrid1_Click()
MsgBox MSFlexGrid1.TextMatrix(MSFlexGrid1.Row, MSFlexGrid1.Col), vbOKOnly, "提示消息"
End Sub
MSFlexGrid1.Row,表格中的當(dāng)前行
MSFlexGrid1.Col,表格中的當(dāng)前列
'初始化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'實(shí)現(xiàn)MSFlexGrid控件,單行背景為淺灰,雙行為蘭色
Dim j As Integer
With MSFlexGrid1
.AllowBigSelection = True ' 設(shè)置網(wǎng)格樣式
.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:\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上點(diǎn)擊右鍵,選擇屬性,選擇"選擇模式",自己任選
點(diǎn)擊得到行代碼
msgrid1.Col就是你選擇的列號(hào)
msgrid1.Row 就是你選擇的行號(hào)
msgrid1.text就是你選擇單元格的內(nèi)容。
>> 將文本賦值給MsFlexGrid的單元格
MsFlexGrid.TextMatrix(3,1)=”Hello”
>> 在MsFlexGrid控件單元格中插入背景圖形
Set MsFlexGrid.CellPicture=LoadPicture(“C:\temp\1.bmp”)
>>選中某個(gè)單元
MsFlexGrid.Row=1
MsFlexGrid.Col=1
>>用粗體格式化當(dāng)前選中單元
MsFlexGrid.CellFontBold=True
>> 添加新的一行
使用AddItem方法,用Tab字符分開不同單元格的內(nèi)容
dim row as string
row=”AAA”&vbtab&”bbb”
MsFlexFrid1.addItem row
>>怎樣來(lái)實(shí)現(xiàn)MSFlexGrid控件單數(shù)行背景為白色,雙數(shù)的行背景為藍(lán)色?
Dim i As Integer
With MSFlexGrid1
.AllowBigSelection = True ’ 設(shè)置網(wǎng)格樣式
.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有無(wú)滾動(dòng)條
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 ’判斷水平滾動(dòng)條的可見性
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 ’判斷垂直滾動(dòng)條的可見性
Dim i As Long
HeScroll = False
i = GetScrollRange(MshGrid.hWnd, SB_VERT, lpMinPos, lpMaxPos)
If lpMaxPos <> lpMinPos Then HeScroll = True
End Function
>>程序運(yùn)行時(shí),想動(dòng)態(tài)增加MSFlexgrid的列數(shù)
在第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
>> 請(qǐng)教MSFlexGrid中的對(duì)齊功能的使用
設(shè)置MSFlexGrid1.ColAlignment(index)=n
>>得到MSFlexGrid控件中當(dāng)前選中的一行
msflexgrid1.rowsel就是當(dāng)前選中行
>> 如何通過(guò)代碼調(diào)節(jié)列寬度
msflexgrid1.colwidth(i)=4000
--------------------------------------------------------------------------------------
'OutDataToText
'將MsFlexGrid控件中顯示的內(nèi)容輸出到文本文件
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 "導(dǎo)出成功"
Ert:
MsgBox Err.Description
Me.MousePointer = 0
End Sub
增加 MsFlexGrid 的編輯功能
概述
MsFlexGrid 控件沒有提供文本編輯的功能,下面的例子演示了如何利用一個(gè)TextBox 實(shí)現(xiàn)編輯當(dāng)前網(wǎng)格的功能。
在按下一個(gè)鍵后, 就把TextBox 移動(dòng)到當(dāng)前的位置, 并激活。 在鍵入回車或移動(dòng)到其他網(wǎng)格時(shí),
就把TextBox 中的內(nèi)容放到網(wǎng)格中。
實(shí)現(xiàn)步驟
1 打開 VB5, 開啟一個(gè)新的工程。
2 在菜單“工程” 中選擇 “部件”, 在列表中選中 “Microsoft FlexGrid Control ..”
3 放一個(gè) MsFlexGrid 控件和一個(gè)TextBox 控件(Text1)到 Form1。 修改MsFlexGrid 控件的名稱為 Grid1,
設(shè)置Grid1 的行,列 為 4, 固定行,列為 0。 設(shè)置 Text1 的 Visiable 為 False, BorderStyle 為
None(0)。
4 在Form1 的代碼中增加聲明:
Const ASC_ENTER = 13 '回車
Dim gRow As Integer
Dim gCol As Integer
5 增加代碼到 Grid_KeyPress 過(guò)程:
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 過(guò)程:
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 過(guò)程:
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 開始測(cè)試。 您可以自由地在 Grid 中移動(dòng), 按回車可以開始或結(jié)束編輯。
使用MsFlexGrid控件的幾個(gè)函數(shù)
作者:中國(guó)論壇網(wǎng)收集 來(lái)源:http://www.51one.net 加入時(shí)間:2004-8-25
在VB處理數(shù)據(jù)顯示的時(shí)候,使用表格是一種好的方法,雖然DataGrid可以與數(shù)據(jù)源綁定,但是總有美中不足,就是
外觀不好看,所以有時(shí)應(yīng)用MsFlexGrid顯示數(shù)據(jù)還是一種比較好的方法,以下幾個(gè)函數(shù)是用來(lái)控制MsFlexGrid的程序
(本人語(yǔ)言表達(dá)能力有限,還請(qǐng)見諒)
''MsFlexGrid操作函數(shù)
''合并列
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 "對(duì)不起,行列設(shè)置錯(cuò)誤!", 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 "對(duì)不起,行列設(shè)置錯(cuò)誤!", 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
''轉(zhuǎn)換索引
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 "對(duì)不起,行列設(shè)置錯(cuò)誤!", 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 "對(duì)不起,行列設(shè)置錯(cuò)誤!", 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 "對(duì)不起,行列設(shè)置錯(cuò)誤!", vbOKOnly, App.Title
GetItem = ""
Exit Function
End If
GetItem = GridObj.TextArray(faIndex(GridObj, row, col))
End Function
在msflexgrid控件中每一個(gè)cell格的內(nèi)容是不可以由用戶直接編輯的但是我們可以通過(guò)一些小技
巧來(lái)方便的實(shí)現(xiàn)這編輯功能來(lái)擴(kuò)展msflexgrid的應(yīng)用(在實(shí)際應(yīng)用中這是很常用的功能)。
你只需按下面的做即可輕松實(shí)現(xiàn)編輯msflexgrid控件數(shù)據(jù)的功能
例在窗體上放一文本框txtvalue,和一msflexgrid控件grid
‘文本框控件的keypress事件
private sub txtvalue_keypress(keyascii as integer)
‘放入一些處理過(guò)程,如只需輸入數(shù)字時(shí)的處理
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
'當(dāng)用戶輸入數(shù)據(jù)時(shí)直接調(diào)用文本框的keypress事件
private sub grid_keypress(keyascii as integer)
txtvalue_keypress keyascii
end sub
ok,這樣一個(gè)可編輯的msflexgrid控件就完成了,簡(jiǎn)單吧!!
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("真的刪除當(dāng)前記錄?", vbInformation + vbYesNo, "警告")
If response = vbYes Then
Ssql = "delete from login where loginname='" & sphy & "'"
gcnLeaguers.Execute (Ssql)
MsgBox "刪除成功!", vbInformation + vbOKOnly, "系統(tǒng)提示"
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
原理
當(dāng)用戶點(diǎn)擊msflexgrid中的某個(gè)cell格要輸入數(shù)據(jù)時(shí),產(chǎn)生entercell事件,在這里我們對(duì)文本
框進(jìn)行初始化,輸入當(dāng)前cell格中的內(nèi)容,并且選中所有文本。當(dāng)用戶要按下按鍵進(jìn)行輸入時(shí),就直
接調(diào)用txtvalue的事件,由文本框來(lái)處理.
處理的結(jié)果同grid的當(dāng)前cell同步,使用戶編輯cell格就象使用文本框一樣方便。
網(wǎng)格單元格中文字的多行顯示很簡(jiǎn)單,只要把WordWarp屬性改為True就可以了。
1.VB6自帶MSHFlexGrid只支持2048條記錄顯示,這個(gè)問題通過(guò)裝VB6 的SP6解決.
2.TopRow屬性的使用,這個(gè)屬性可以使用代碼移動(dòng)MSHFlexGrid的行顯示,如要做一個(gè)從上至下的滾動(dòng)顯示,使用這個(gè)屬性解決起來(lái)非常方便
3.使用選碼高亮即選中顯示某一行,如要選中第三行,可以使用代碼:
MSHFlexGrid1.Row = 3
MSHFlexGrid1.TopRow =3
myMSHFlexGrid.ColSel = MSHFlexGrid1.Cols - 1
MSHFlexGrid中單擊某一行變色,如何實(shí)現(xiàn)?
Option Explicit
Public j As Long
Private Sub RowColor(i As Long, j As Long)
'i代表現(xiàn)在點(diǎn)的這一行
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

TA貢獻(xiàn)1851條經(jīng)驗(yàn) 獲得超4個(gè)贊
i的循環(huán)條件是從1到3,換句話說(shuō)循環(huán)的退出條件就是i大于3,所以當(dāng)循環(huán)結(jié)束時(shí),i的值就是3+step,由于step默認(rèn)為1,所以i就等于4咯
補(bǔ)充說(shuō)明一下:next
i的作用相當(dāng)于i=i+step,當(dāng)最后一次循環(huán)運(yùn)行時(shí),i=3,那么當(dāng)運(yùn)行到next時(shí),i就被自動(dòng)+step,由于step是1,所以i就變?yōu)?了。這個(gè)可能會(huì)讓你更容易理解。
- 2 回答
- 0 關(guān)注
- 127 瀏覽
添加回答
舉報(bào)