QUBi
Aug 20th, 2000, 11:17 AM
I ahve recenlty downloaded an sample project to read ini files.
What I would like to be able to do is populate various list boxes or combo boxes with the various oprions in the ini file.
for example I would firstly like to get a list of the sections in brackets, then after selecting this a list of the parameter under that section.
Is this at all possible.
Thnx for any help you can extend.
~qb~
parksie
Aug 20th, 2000, 11:32 AM
If you're going to do it that way, it's probably easier to read in the whole file and parse it yourself, that way you can instantly get a tree-based structure representing the file.
As parksie suggested, that is probably a faster way to do it and here's how:
Public Sub List_Add(List As ComboBox, txt As String)
List.AddItem txt
End Sub
Public Sub List_Load(TheList As ListBox, FileName As String)
On Error Resume Next
Dim TheContents As String
Dim fFile As Integer
fFile = FreeFile
Open FileName For Input As fFile
Do
Line Input #fFile, TheContents$
Call List_Add(TheList, TheContents$)
Loop Until EOF(fFile)
Close fFile
End Sub
Public Sub List_Save(TheList As ListBox, FileName As String)
On Error Resume Next
Dim Save As Long
Dim fFile As Integer
fFile = FreeFile
Open FileName For Output As fFile
For Save = 0 To TheList.ListCount - 1
Print #fFile, TheList.List(Save)
Next Save
Close fFile
End Sub
But here is how to read an ini file anyway.
Public Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
Public Function ReadINI(strsection As String, strkey As String, strfullpath As String) As String
Dim strbuffer As String
Let strbuffer$ = String$(750, Chr$(0&))
Let ReadINI$ = Left$(strbuffer$, GetPrivateProfileString(strsection$, ByVal LCase$(strkey$), "", strbuffer, Len(strbuffer), strfullpath$))
End Function
Usage:
'x = readini("test.ini", "test", "C:\test.ini")
'List1.Additem x
See this tip (http://www.vb-world.net/tips/tip17.html)
Crazy D
Aug 21st, 2000, 04:12 AM
Here's an ini class I wrote a time ago:
http://codeguru.earthweb.com/vb/articles/1772.shtml