saving settings

cdoverlaw

Well-known member
Joined
Oct 11, 2003
Messages
146
how do i get a vb.net program to save settings (like which CheckBoxs are checked) into a text file and then load them when the program starts

Jonathan
 
Serialization is a process of saving a state of an object to a file with exact properties that were set to it during serialization. Its like putting an object into a file for storage :).
 
Why do u want to save them into a text file? Cant you save the settings into a table inside a database? If yes, ill teach you how.
Mohsen
 
I dont mind what way they are saved, so any help you can offer would be realy appreciated, i just need all settings to be saved so that the user dosent have to change their settings every time they launch the program

Jonathan
 
Listen Jonathan, the professional way to do what you want is by connecting you application to a database, save everything the user does in an organized tables, and then on load you take the informatoin from the database. But now Im not gonna teach you that because it need time, so Ill teach you how to write the settings into a file. Try the following:


Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.Invoke(New SettingInvoke(AddressOf Me.GetSetting))
SettingThread.Start()
GetSetting()

End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim FileWritrer As New IO.StreamWriter("C:\SavingSetting.txt")
FileWritrer.WriteLine(" CheckBox2," & CheckBox2.Checked)
FileWritrer.WriteLine(" " & TextBox1.Text)
FileWritrer.Flush() make sure everything is dumped into the file
FileWritrer.Close()

End Sub

Private SettingThread As New Threading.Thread(AddressOf GetSetting)

Private Sub GetSetting()

Dim FileReader As New IO.StreamReader("C:\SavingSetting.txt")
Dim i As Integer = 0
Dim s, e As String
Dim x, y As Integer
While FileReader.Read() <> -1
If i = 0 Then
s = FileReader.ReadLine()
s = s.Substring(s.LastIndexOf(",") + 1)
If s.Equals("False") Then
CheckBox2.Checked = False
Else
CheckBox2.Checked = True
End If
Else
TextBox1.Text = FileReader.ReadLine()
End If
i = i + 1
End While
FileReader.Close()
SettingThread.Abort()
End Sub

once you click on button2, the information are saved on a file on the C drive. Once you load the form, the settings are loaded again.
 
Yup, like i thought, i am having problems,
I have attached my program, (without any of the saving setting code)
Well the first problem is that when i use the code
Code:
Me.Invoke(New SettingInvoke(AddressOf Me.GetSetting))
 SettingThread.Start()
GetSetting()
I get an error with the GetSetting() saying
C:\Documents and Settings\Jonathan.N-FORCE\My Documents\Visual Studio Projects\docked sidebar\XPEBarTest\Sidebar.vb(620): Argument not specified for parameter AppName of Public Function GetSetting(AppName As String, Section As String, Key As String, [Default As String = ""]) As String.

C:\Documents and Settings\Jonathan.N-FORCE\My Documents\Visual Studio Projects\docked sidebar\XPEBarTest\Sidebar.vb(620): Argument not specified for parameter Key of Public Function GetSetting(AppName As String, Section As String, Key As String, [Default As String = ""]) As String.

C:\Documents and Settings\Jonathan.N-FORCE\My Documents\Visual Studio Projects\docked sidebar\XPEBarTest\Sidebar.vb(620): Argument not specified for parameter Section of Public Function GetSetting(AppName As String, Section As String, Key As String, [Default As String = ""]) As String.
 

Attachments

Back
Top