I have been trying for a while. Can anybody help me.
I want to load the contents of an INI file into a listbox, and I want the INI file to load, and save any changes on startup and exit respectively automatically.
Can anyone help?
Cheers
Phil
Printable View
I have been trying for a while. Can anybody help me.
I want to load the contents of an INI file into a listbox, and I want the INI file to load, and save any changes on startup and exit respectively automatically.
Can anyone help?
Cheers
Phil
Yes, I recently found a Class module on the net which implements INI file handling. I tested it and it seems to work too!
Here are the author's data:
Name: CINIFile (CINIFILE.CLS)
Type: Utility API wrapper class
Description: An "object-oriented" approach to using WIndows INI files, with some useful additions.
Author: Klaus H. Probst [[email protected]]
URL: http://www.vbbox.com/
I tried this... very complicated...cheers for it though!
Can anybody suggest any other ways of doing this. I am new(ish) to VB, and still need a bit of help......
Cheers,
Phil
If the keyname in the INI fil is number :
[data]
1=data1
2=data2
3=data3
it,s easy :
Code:
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
'Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpString As String, ByVal lpFileName As String) As Long
Sub loadLIST ()
Dim loadedNumber As Integer
Dim URL1 As String
loadedNumber=1
URL1= "c:\windows\win.ini" ' Path of the ini file
NextOne:
If Not GetPrivateProfileString("Data", loadedNumber, "", sData, Len(sData), URL1)="" Then
List1.AddItem GetPrivateProfileString("Data", loadedNumber, "", sData, Len(sData), URL1)
loadedNumber = loadedNumber + 1
GoTo NextOne
End if
End Sub
Here is an easy example using INI files that may help you to understand better.
Code: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 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 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
Public Sub WriteINI(strsection As String, strkey As String, strkeyvalue As String, strfullpath As String)
Call WritePrivateProfileString(strsection$, UCase$(strkey$), strkeyvalue$, strfullpath$)
End Sub
Usage
'Write
Call WriteINI("Value.ini", "MyValue", "TheValue", "C:\Value.ini")
'Read
x = ReadINI"Value.ini", "MyValue", "C:\Value.ini")
Msgbox x
This is in case you where discourged from the INI file
That just writes it to any file name you want .Code:Sub ListBoxLoad(File As String, ListBox As ListBox)
'USAGE: LOADLIST(LIST1,"STUFF.LST")
'THAT WILL LOAD THE CONTENTS OF STUFF.LST
On Error Resume Next
Dim free%, G$
free = FreeFile
ListBox.Clear
Open File For Input As #free
Do Until EOF(free)
Line Input #free, G$
ListBox.AddItem G$
Loop
Close free
End Sub
Public Sub ListBoxSave(File As String, List As ListBox)
On Error Resume Next
Dim free%
free = FreeFile
Dim SaveList As Long
Open File For Output As #free
For SaveList& = 0 To List.ListCount - 1
Print #free, List.List(SaveList&)
Next SaveList&
Close #free
Finish:
End Sub
In fact, the class file is not as difficult as it seems.
My first impression was also something like yours. But if you look more closer to the code, it's quite simple to do some INI operations.
You could start with declaring an instance of the class, like:
Code:Option Explicit
Dim clsINI As New CINIFile
Private Sub Form_Load()
clsINI.File = "Win.INI"
End Sub
Private Sub Form_Resize()
Dim Sections() As String
Dim i As Integer
Erase Sections
Me.Cls
clsINI.EnumSections Sections
For i = 0 To UBound(Sections)
Me.Print Sections(i)
Next i
End Sub
But this wasn't able to retrieve the rest of the keys, if this occurQuote:
Originally posted by pro2
If the keyname in the INI fil is number :
[data]
1=data1
2=data2
3=data3
it,s easy :
Code:
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
'Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpString As String, ByVal lpFileName As String) As Long
Sub loadLIST ()
Dim loadedNumber As Integer
Dim URL1 As String
loadedNumber=1
URL1= "c:\windows\win.ini" ' Path of the ini file
NextOne:
If Not GetPrivateProfileString("Data", loadedNumber, "", sData, Len(sData), URL1)="" Then
List1.AddItem GetPrivateProfileString("Data", loadedNumber, "", sData, Len(sData), URL1)
loadedNumber = loadedNumber + 1
GoTo NextOne
End if
End Sub
1=key1
2=
3=key3
where key no 2 is blank.
Does this help?
To load:
To save:Code:listbox.Clear
Open INIfile For Input as 1
Do Until Eof(1)
Line Input #1, Text$
listbox.AddItem Text$
Loop
Close 1
If this is not what you mean, you could look up these API functions:Code:Open INIfile For Output as 1
For LineNr = 0 to listbox.ListCount - 1
Print #1, listbox.List(LineNr)
Next LineNr
Close 1
GetPrivateProfileInt
GetPrivateProfileString
GetProfileInt
GetProfileString
WritePrivateProfileString
WriteProfileString
well, you can try to have a key that contain the numbers of item that you want to load in your list, and then, you read this key and make a loop to load everything. with this method, you don't have to validate if the key is empty to end the loading process.Quote:
Originally posted by Harddisk
But this wasn't able to retrieve the rest of the keys, if this occur
1=key1
2=
3=key3
where key no 2 is blank.
hope this help, if you need a code example, drop me an e-mail at disco_boul****@email.com
see ya