湖南发展集团王修林:可编辑的MSFlexGrid:

来源:百度文库 编辑:中财网 时间:2024/04/29 16:09:51
Private Sub Form_Load()
Dim fs As New FileSystemObject
Dim txtf As TextStream
Dim path As String, Line As String
Dim Cells() As String, Idx As Integer

path = App.path
If Right(path, 1) <> "" Then path = path & ""
Set txtf = fs.OpenTextFile(path & "score.txt", ForReading)

With MSFlexGrid1
.AllowUserResizing = flexResizeColumns ' 可以改变列宽
.Cols = 5 ' 列数 = 5
.Rows = 1 ' 行数暂订 1
.FixedCols = 0 ' 固定列 = 0

' 设置标题
.TextMatrix(0, 0) = "学号"
.TextMatrix(0, 1) = "姓名"
.TextMatrix(0, 2) = "语文"
.TextMatrix(0, 3) = "数学"
.TextMatrix(0, 4) = "英语"

.ColAlignment(0) = flexAlignRightCenter
.ColAlignment(2) = flexAlignRightCenter
.ColAlignment(3) = flexAlignRightCenter
.ColAlignment(4) = flexAlignRightCenter
End With


Idx = 1
While Not txtf.AtEndOfStream
Line = txtf.ReadLine
Cells = Split(Line, ",")
MSFlexGrid1.Rows = MSFlexGrid1.Rows + 1 ' 增加一行
' 将Cells(0°4) 指定给TextMatrix(Idx, 0°4)
For I = 0 To UBound(Cells)
MSFlexGrid1.TextMatrix(Idx, I) = Cells(I)
Next
Idx = Idx + 1
Wend
txtf.Close
End Sub


Private Sub MSFlexGrid1_Click()
Dim c As Integer, r As Integer

With MSFlexGrid1
c = .Col: r = .Row
Text1.Left = .Left + .ColPos©
Text1.Top = .Top + .RowPos®
If .Appearance = 1 Then
Text1.Left = Text1.Left + 2 * Screen.TwipsPerPixelX
Text1.Top = Text1.Top + 2 * Screen.TwipsPerPixelY
End If
Text1.Width = .ColWidth©
Text1.Height = .RowHeight®

Text1.Text = .Text
End With
Text1.Visible = True
Text1.SetFocus
End Sub


Private Sub MSFlexGrid1_Scroll()
Text1.Visible = False
End Sub


Private Sub Text1_Change()
MSFlexGrid1.Text = Text1.Text
End Sub


Private Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyReturn Then ' 按下 Enter 键
Text1.Visible = False
End If
End Sub


Private Sub Text1_LostFocus()
Text1.Visible = False
End Sub



其中score.txt文件内容为:


850301,陈桶一,90,76,98
850302,黄光权,58,77,75
850303,胡生妙,41,14,33
850304,王为全,95,97,87
850305,李日正,59,66,57
850306,刘德昌,28,11,33
850307,方正一,98,100,100
850308,刘康保,0,0,10
850309,谢掬花,95,74,89
850310,王美兰,41,46,49
850311,徐小当,91,99,84
850312,叶小毛,0,10,0
850313,林东海,92,92,88
850314,林大阳,35,19,29
850315,钟德级,98,90,91
850316,刘力锦,47,50,55
850317,方小房,94,94,100
850318,刘纪成,79,85,77
850319,李普三,84,74,58
850320,刘一心,54,57,64
850321,方日通,83,80,94
850322,刘顺成,71,65,73
850323,谢利利,20,25,10
850324,王涵合,93,100,90
850325,徐佳佳,68,56,39
850326,李红仁,25,24,0
850327,林大玉,87,77,92
850328,林玉成,64,90,59
850329,钟百达,0,0,20
Edited by: LionCSQ