-
I'm fairly new to VB and I have a program that has 13 listboxes. I want it so that when i close the program, all the listboxes' contents are saved to an ini file, and when the program is opened all the conents are restored to the listboxes from the ini file. how would i do this?
-
Private Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
Private Declare Function WritePrivateProfileString _
Lib "kernel32" Alias "WritePrivateProfileStringA" _
(ByVal lpApplicationName As String, _
ByVal lpKeyName As Any, _
ByVal lpString As Any, _
ByVal lpFileName As String) As Long
Public Function ReadIniFile(lpAppName$, lpKeyName$, lpFileName$, lpDefault$) As Variant
Dim lpReturnString$, Size%, Valid%
lpReturnString$ = Space$(128)
Size% = Len(lpReturnString$)
Valid% = GetPrivateProfileString(lpAppName$, lpKeyName$, lpDefault$, lpReturnString$, Size%, lpFileName$)
ReadIniFile = Strip(lpReturnString$)
End Function
Public Sub WriteIniFile(strSection As String, strKey As String, strValue As String, strINI As String)
WritePrivateProfileString strSection, strKey, strValue, strINI
End Sub
Private Function Strip(strString As String) As String
Dim intPos As Integer
Dim strTemp As String
intPos = InStr(1, strString, vbNullChar)
Strip = Left(strString, intPos - 1)
End Function
'In the form unload event,
WriteIniFile "Contents", "ListBox1", listbox1.text, strINIFile
'In the form load event,
listbox1.text = ReadIniFile("Contents", "ListBox1", strINIFile, "")
-
See This link for saving and loading INI files.