[Solved]Save preferences

Posts 1–10 of 10 · Page 1 of 1
[Solved]Save preferences
hey i am making a project when you get into form 2 It needs to save all the stuff that's in there like check boxes checked, text boxes stuff like that
I don't understand what you're doing ? Are you saying that when you get on to Form 2, all the stuff on the Form1 is to be saved ??

Unless you are clear on what you exactly want to do, take a look at this thread:


http://www.mpgh.net/forum/33-visual-...gs-solved.html

It covers almost all the savesettings methods.
I didnt get what u emant to say dude.......Can u explain a bit more eh?
Use the my.settings or an .ini file.
Quote Originally Posted by Zoom View Post
Use the my.settings or an .ini file.
how can i create a .ini file?
Quote Originally Posted by Sixx93 View Post
how can i create a .ini file?
First create a module and put the following function in it (Reads settings from an INI file):

Code:
Public Function ReadIniValue(ByRef INIpath As String, ByRef KEY As String, ByRef Variable As String) As String
		Dim NF As Short
		Dim Temp As String
		Dim LcaseTemp As String
		Dim ReadyToRead As Boolean
        NF = FreeFile()
		ReadIniValue = ""
		KEY = "[" & LCase(KEY) & "]"
		Variable = LCase(Variable)
        FileOpen(NF, INIpath, OpenMode.Binary)
		FileClose(NF)
		SetAttr(INIpath, FileAttribute.Archive)
        FileOpen(NF, INIpath, OpenMode.Input)
		While Not EOF(NF)
			Temp = LineInput(NF)
			LcaseTemp = LCase(Temp)
			If InStr(LcaseTemp, "[") <> 0 Then ReadyToRead = False
			If LcaseTemp = KEY Then ReadyToRead = True
			If InStr(LcaseTemp, "[") = 0 And ReadyToRead = True Then
				If InStr(LcaseTemp, Variable & "=") = 1 Then
					ReadIniValue = Mid(Temp, 1 + Len(Variable & "="))
                    FileClose(NF)
                    Exit Function
				End If
			End If
		End While
		FileClose(NF)
	End Function
Then add this function (Writes settings to INI file):

Code:
Public Function WriteIniValue(ByRef INIpath As String, ByRef PutKey As String, ByRef PutVariable As String, ByRef PutValue As String) As Object
		Dim Temp As String
		Dim LcaseTemp As String
		Dim ReadKey As String
		Dim ReadVariable As String
		Dim LOKEY As Short
		Dim HIKEY As Short
		Dim KEYLEN As Short
		Dim VAR As Short
		Dim VARENDOFLINE As Short
		Dim NF As Short
		Dim X As Short
		
AssignVariables: 
		NF = FreeFile
		ReadKey = vbCrLf & "[" & LCase(PutKey) & "]" & Chr(13)
		KEYLEN = Len(ReadKey)
		ReadVariable = Chr(10) & LCase(PutVariable) & "="
		
EnsureFileExists: 
		FileOpen(NF, INIpath, OpenMode.Binary)
		FileClose(NF)
		SetAttr(INIpath, FileAttribute.Archive)
		
LoadFile: 
		FileOpen(NF, INIpath, OpenMode.Input)
		Temp = InputString(NF, LOF(NF))
		Temp = vbCrLf & Temp & "[]"
		FileClose(NF)
		LcaseTemp = LCase(Temp)
		
LogicMenu: 
		LOKEY = InStr(LcaseTemp, ReadKey)
		If LOKEY = 0 Then GoTo AddKey
		HIKEY = InStr(LOKEY + KEYLEN, LcaseTemp, "[")
		VAR = InStr(LOKEY, LcaseTemp, ReadVariable)
		If VAR > HIKEY Or VAR < LOKEY Then GoTo AddVariable
		GoTo RenewVariable
		
AddKey: 
		Temp = Left(Temp, Len(Temp) - 2)
		Temp = Temp & vbCrLf & vbCrLf & "[" & PutKey & "]" & vbCrLf & PutVariable & "=" & PutValue
		GoTo TrimFinalString
		
AddVariable: 
		Temp = Left(Temp, Len(Temp) - 2)
		Temp = Left(Temp, LOKEY + KEYLEN) & PutVariable & "=" & PutValue & vbCrLf & Mid(Temp, LOKEY + KEYLEN + 1)
		GoTo TrimFinalString
		
RenewVariable: 
		Temp = Left(Temp, Len(Temp) - 2)
		VARENDOFLINE = InStr(VAR, Temp, Chr(13))
		Temp = Left(Temp, VAR) & PutVariable & "=" & PutValue & Mid(Temp, VARENDOFLINE)
		GoTo TrimFinalString
		
TrimFinalString: 
		Temp = Mid(Temp, 2)
		Do Until InStr(Temp, vbCrLf & vbCrLf & vbCrLf) = 0
			Temp = Replace(Temp, vbCrLf & vbCrLf & vbCrLf, vbCrLf & vbCrLf)
		Loop 
		
		Do Until Right(Temp, 1) > Chr(13)
			Temp = Left(Temp, Len(Temp) - 1)
		Loop 
		
		Do Until Left(Temp, 1) > Chr(13)
			Temp = Mid(Temp, 2)
		Loop 
		
OutputAmendedINIFile: 
		FileOpen(NF, INIpath, OpenMode.Output)
		PrintLine(NF, Temp)
		FileClose(NF)
    End Function
To demonstrate its working:

Create a textbox and a button on the form. Now double click the button to open its click event code. Now add the following code to it:


Code:
WriteIniValue("D:\IniTest.ini", "Default", "Text", (TextBox1.Text))
This will save the text of the Textbox1 into the ini file "D:\IniTest.ini" with the category "Default" and variable "Text".

Now double click the form or simply goto its load event code. There, add the following line of code:


Code:
TextBox1.Text = ReadIniValue("D:\IniTest.ini", "Default", "Text")
This will read the text from the ini file "D:\IniTest.ini" with the category "Default" and variable "Text" and write it into Textbox1.

Hope this helps !!
Quote Originally Posted by Hassan View Post
First create a module and put the following function in it (Reads settings from an INI file):

Code:
Public Function ReadIniValue(ByRef INIpath As String, ByRef KEY As String, ByRef Variable As String) As String
		Dim NF As Short
		Dim Temp As String
		Dim LcaseTemp As String
		Dim ReadyToRead As Boolean
        NF = FreeFile()
		ReadIniValue = ""
		KEY = "[" & LCase(KEY) & "]"
		Variable = LCase(Variable)
        FileOpen(NF, INIpath, OpenMode.Binary)
		FileClose(NF)
		SetAttr(INIpath, FileAttribute.Archive)
        FileOpen(NF, INIpath, OpenMode.Input)
		While Not EOF(NF)
			Temp = LineInput(NF)
			LcaseTemp = LCase(Temp)
			If InStr(LcaseTemp, "[") <> 0 Then ReadyToRead = False
			If LcaseTemp = KEY Then ReadyToRead = True
			If InStr(LcaseTemp, "[") = 0 And ReadyToRead = True Then
				If InStr(LcaseTemp, Variable & "=") = 1 Then
					ReadIniValue = Mid(Temp, 1 + Len(Variable & "="))
                    FileClose(NF)
                    Exit Function
				End If
			End If
		End While
		FileClose(NF)
	End Function
Then add this function (Writes settings to INI file):

Code:
Public Function WriteIniValue(ByRef INIpath As String, ByRef PutKey As String, ByRef PutVariable As String, ByRef PutValue As String) As Object
		Dim Temp As String
		Dim LcaseTemp As String
		Dim ReadKey As String
		Dim ReadVariable As String
		Dim LOKEY As Short
		Dim HIKEY As Short
		Dim KEYLEN As Short
		Dim VAR As Short
		Dim VARENDOFLINE As Short
		Dim NF As Short
		Dim X As Short
		
AssignVariables: 
		NF = FreeFile
		ReadKey = vbCrLf & "[" & LCase(PutKey) & "]" & Chr(13)
		KEYLEN = Len(ReadKey)
		ReadVariable = Chr(10) & LCase(PutVariable) & "="
		
EnsureFileExists: 
		FileOpen(NF, INIpath, OpenMode.Binary)
		FileClose(NF)
		SetAttr(INIpath, FileAttribute.Archive)
		
LoadFile: 
		FileOpen(NF, INIpath, OpenMode.Input)
		Temp = InputString(NF, LOF(NF))
		Temp = vbCrLf & Temp & "[]"
		FileClose(NF)
		LcaseTemp = LCase(Temp)
		
LogicMenu: 
		LOKEY = InStr(LcaseTemp, ReadKey)
		If LOKEY = 0 Then GoTo AddKey
		HIKEY = InStr(LOKEY + KEYLEN, LcaseTemp, "[")
		VAR = InStr(LOKEY, LcaseTemp, ReadVariable)
		If VAR > HIKEY Or VAR < LOKEY Then GoTo AddVariable
		GoTo RenewVariable
		
AddKey: 
		Temp = Left(Temp, Len(Temp) - 2)
		Temp = Temp & vbCrLf & vbCrLf & "[" & PutKey & "]" & vbCrLf & PutVariable & "=" & PutValue
		GoTo TrimFinalString
		
AddVariable: 
		Temp = Left(Temp, Len(Temp) - 2)
		Temp = Left(Temp, LOKEY + KEYLEN) & PutVariable & "=" & PutValue & vbCrLf & Mid(Temp, LOKEY + KEYLEN + 1)
		GoTo TrimFinalString
		
RenewVariable: 
		Temp = Left(Temp, Len(Temp) - 2)
		VARENDOFLINE = InStr(VAR, Temp, Chr(13))
		Temp = Left(Temp, VAR) & PutVariable & "=" & PutValue & Mid(Temp, VARENDOFLINE)
		GoTo TrimFinalString
		
TrimFinalString: 
		Temp = Mid(Temp, 2)
		Do Until InStr(Temp, vbCrLf & vbCrLf & vbCrLf) = 0
			Temp = Replace(Temp, vbCrLf & vbCrLf & vbCrLf, vbCrLf & vbCrLf)
		Loop 
		
		Do Until Right(Temp, 1) > Chr(13)
			Temp = Left(Temp, Len(Temp) - 1)
		Loop 
		
		Do Until Left(Temp, 1) > Chr(13)
			Temp = Mid(Temp, 2)
		Loop 
		
OutputAmendedINIFile: 
		FileOpen(NF, INIpath, OpenMode.Output)
		PrintLine(NF, Temp)
		FileClose(NF)
    End Function
To demonstrate its working:

Create a textbox and a button on the form. Now double click the button to open its click event code. Now add the following code to it:


Code:
WriteIniValue("D:\IniTest.ini", "Default", "Text", (TextBox1.Text))
This will save the text of the Textbox1 into the ini file "D:\IniTest.ini" with the category "Default" and variable "Text".

Now double click the form or simply goto its load event code. There, add the following line of code:


Code:
TextBox1.Text = ReadIniValue("D:\IniTest.ini", "Default", "Text")
This will read the text from the ini file "D:\IniTest.ini" with the category "Default" and variable "Text" and write it into Textbox1.

Hope this helps !!
ty alot!!! u are very helpfull!
hey flame i was wondering what is the address or path for you desktop

i.e C://desktop
Quote Originally Posted by hopefordope View Post
hey flame i was wondering what is the address or path for you desktop

i.e C://desktop
Just put this:

My.Computer.Filesystem.SpecialDirectories.Desktop & "\Example.ini"

@Lolland: Only filename is needed. Stream will be created automatically.
Put this code in:

Code:
Dim stream1 as system.io.streamwriter = new system.io.streamwriter(my.computer.filesystem.specialdirectories.desktop & "\example.ini")
stream1.write("preferenceshere")
stream1.close
Posts 1–10 of 10 · Page 1 of 1
This thread is closed for replies.

Similar Threads

Tags for this Thread

None

Need help?