MSFlexGrid單元格值保存
如果将MSFlexGrid單元格的值保存,下次打开指定的值!
MSFlexGrid.Cols = 5
Rows不确定,我想保存單元格的值,下次打时还是那个值,可否写入txt文件?
txt文件也有4列!
[解决办法]
Private Sub Command1_Click()
'保存单元格内容,不包含固定行固定列
Dim i As Long, j As Long
Dim s As String
Open "e:\m.txt " For Output As #1
With MSHFlexGrid1
For i = .Rows - 1 To 1 Step -1
s = " "
For j = 1 To .Cols - 1
s = s & vbTab & .TextMatrix(i, j)
Next
Print #1, s
Next
End With
Close 1
End Sub
Private Sub Command2_Click()
'从文件读取内容,恢复到单元格
Dim s As String
MSHFlexGrid1.Rows = 2
Open "e:\m.txt " For Input As #1
Do While Not EOF(1)
Line Input #1, s
MSHFlexGrid1.AddItem s, 1
Loop
MSHFlexGrid1.Rows = MSHFlexGrid1.Rows - 1
End Sub
[解决办法]
'窗体代码
Private Sub cmdsave_Click()
MSHFlexGrid1.Cols = 5
MSHFlexGrid1.TextMatrix(0, 1) = Text1.Text
MSHFlexGrid1.TextMatrix(0, 2) = Text2.Text
MSHFlexGrid1.TextMatrix(0, 3) = Text3.Text
MSHFlexGrid1.TextMatrix(0, 4) = Text4.Text
str = str & "id1= " & MSHFlexGrid1.TextMatrix(0, 1) & vbCrLf
str = str & "id2= " & MSHFlexGrid1.TextMatrix(0, 2) & vbCrLf
str = str & "id3= " & MSHFlexGrid1.TextMatrix(0, 3) & vbCrLf
str = str & "id4= " & MSHFlexGrid1.TextMatrix(0, 4) & vbCrLf
Call settxt
End Sub
Private Sub cmdsel_Click()
Call Gettxt
MSHFlexGrid1.Cols = 5
MSHFlexGrid1.TextMatrix(0, 1) = id1
MSHFlexGrid1.TextMatrix(0, 2) = id2
MSHFlexGrid1.TextMatrix(0, 3) = id3
MSHFlexGrid1.TextMatrix(0, 4) = id4
End Sub
'模块代码
Global id1 As String
Global id2 As String
Global id3 As String
Global id4 As String
Global str As String
Public Sub Gettxt()
On Error Resume Next
Open getapppath + "\data.txt " For Input As #1
Input #1, id1, id2, id3, id4
Close #1
id1 = Right(id1, Len(id1) - InStr(id1, "= "))
id2 = Right(id2, Len(id2) - InStr(id2, "= "))
id3 = Right(id3, Len(id3) - InStr(id3, "= "))
id4 = Right(id4, Len(id4) - InStr(id4, "= "))
If Err Then
MsgBox Err.Description & vbCrLf & "程序即将退出 ", , "系统登录 "
End
End If
End Sub
Public Function getapppath() As String
If Right(App.Path, 1) = "\ " Then
getapppath = Left(App.Path, Len(App.Path) - 1)
Else
getapppath = App.Path
End If
End Function
Public Sub settxt()
On Error Resume Next
Open getapppath + "\data.txt " For Output As #2
Print #2, str
Close #2
If Err Then
MsgBox Err.Description & vbCrLf & "程序即将退出 ", , "系统登录 "
End
End If
End Sub