|
-
Aug 26th, 2010, 02:29 PM
#1
Thread Starter
Junior Member
Re: Save control´s caption in a txt file
 Originally Posted by minitech
Here's 2 functions:
Code:
Public Sub SaveToFile(ByVal f As Form, ByVal location As String)
Dim sw As New IO.StreamWriter(location)
Dim s As New Stack(Of Control)
s.Push(f)
While s.Count > 0
Dim pc As Control = s.Pop()
For Each c As Control In pc.Controls
sw.WriteLine(GetPath(f, c) & "=" & c.Text)
s.Push(c)
Next
End While
sw.Close()
sw.Dispose()
End Sub
Public Sub RestoreFromFile(ByVal f As Form, ByVal location As String)
Dim sr As New IO.StreamReader(location)
Do
Dim s As String = sr.ReadLine()
If s = "" Then Exit Do
GetControl(f, s.Substring(0, s.IndexOf("="c)).Text = s.Substring(s.IndexOf("="c) + 1)
Loop
sr.Close()
sr.Dispose()
End Sub
Private Function GetControl(ByVal c As Control, ByVal path As String) As Control
Dim i As Integer = path.IndexOf("."c)
If i = -1 Then Return c.Controls(path)
Dim s As String = path.Substring(0, i)
Return GetControl(c.Controls(path), path.Substring(i + 1))
End Function
Private Function GetPath(ByVal f As Form, ByVal c As Control) As String
Dim s As String = c.Name
While c.Parent IsNot f AndAlso c.Parent IsNot Nothing
c = c.Parent
s = c.Name & "." & s
End While
Return s
End Function
To use them in your form:
Code:
'To save
SaveToFile(Me, "data.dat")
'To load
RestoreFromFile(Me, "data.dat")
man i tried to use your code but
but there is an error onthis line :
Code:
GetControl(f, s.Substring(0, s.IndexOf("="c)).Text = s.Substring(s.IndexOf("="c) + 1)
there is an extra parentheses an also the .text is not a property of that
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|