VB6.0如何保存读取多控件的状态
2个回答
展开全部
以下代码可以在窗体关闭时把所有text控件的内容(包括控件数组)保存到程序所在的某个文本文件,在程序启动时则可从该文件读出所存内容并分别赋值到相应的text控件中:
Dim sfn As String
Private Sub Form_Load()
Dim ss As String, s() As String
sfn = App.Path & "\textsave.txt"
If Dir(sfn) <> "" Then
Open sfn For Input As #1
Do Until EOF(1)
Line Input #1, ss
s = Split(ss, "^_^")
If UBound(s) = 2 Then
s(2) = Replace(s(2), "#_#", vbCrLf)
If s(1) <> "" Then
Me.Controls(s(0))(Val(s(1))).Text = s(2)
Else
Me.Controls(s(0)).Text = s(2)
End If
End If
Loop
Close #1
End If
End Sub
Private Sub Form_Unload(Cancel As Integer)
Dim tt As Object, kk As String
If Dir(sfn) <> "" Then Kill sfn
Open sfn For Output As #1
For Each tt In Me.Controls
If TypeName(tt) = "TextBox" Then
kk = Replace(tt.Text, vbCrLf, "#_#")
If VarType(CallByName(Me, tt.Name, VbGet)) = vbObject Then
Print #1, tt.Name & "^_^" & tt.Index & "^_^" & kk
Else
Print #1, tt.Name & "^_^^_^" & kk
End If
End If
Next
Close #1
End Sub
Dim sfn As String
Private Sub Form_Load()
Dim ss As String, s() As String
sfn = App.Path & "\textsave.txt"
If Dir(sfn) <> "" Then
Open sfn For Input As #1
Do Until EOF(1)
Line Input #1, ss
s = Split(ss, "^_^")
If UBound(s) = 2 Then
s(2) = Replace(s(2), "#_#", vbCrLf)
If s(1) <> "" Then
Me.Controls(s(0))(Val(s(1))).Text = s(2)
Else
Me.Controls(s(0)).Text = s(2)
End If
End If
Loop
Close #1
End If
End Sub
Private Sub Form_Unload(Cancel As Integer)
Dim tt As Object, kk As String
If Dir(sfn) <> "" Then Kill sfn
Open sfn For Output As #1
For Each tt In Me.Controls
If TypeName(tt) = "TextBox" Then
kk = Replace(tt.Text, vbCrLf, "#_#")
If VarType(CallByName(Me, tt.Name, VbGet)) = vbObject Then
Print #1, tt.Name & "^_^" & tt.Index & "^_^" & kk
Else
Print #1, tt.Name & "^_^^_^" & kk
End If
End If
Next
Close #1
End Sub
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询