Results 1 to 10 of 10

Thread: Loading Contents to ListBox via INI

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2000
    Posts
    10

    Exclamation

    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

  2. #2
    Fanatic Member Mad Compie's Avatar
    Join Date
    Aug 2000
    Location
    Kuurne (Belgium)
    Posts
    553
    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/

  3. #3

    Thread Starter
    New Member
    Join Date
    Oct 2000
    Posts
    10
    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

  4. #4
    Addicted Member
    Join Date
    Aug 2000
    Posts
    208
    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

  5. #5
    Guest
    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

  6. #6
    Hyperactive Member
    Join Date
    Aug 2000
    Posts
    258
    This is in case you where discourged from the INI file
    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
    That just writes it to any file name you want .
    Visual Basic 6 SP4 on win98se

    QUIT THE RAT RACE BECAUSE YOUR MESSING THE WORLD UP !!!!!

  7. #7
    Fanatic Member Mad Compie's Avatar
    Join Date
    Aug 2000
    Location
    Kuurne (Belgium)
    Posts
    553
    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

  8. #8
    Hyperactive Member
    Join Date
    Mar 2001
    Posts
    485
    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
    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.

  9. #9
    Frenzied Member
    Join Date
    Feb 2003
    Posts
    1,945
    Does this help?

    To load:

    Code:
    listbox.Clear
    Open INIfile For Input as 1
     Do Until Eof(1)
      Line Input #1, Text$
      listbox.AddItem Text$
     Loop
    Close 1
    To save:

    Code:
    Open INIfile For Output as 1
     For LineNr  = 0 to listbox.ListCount - 1
      Print #1, listbox.List(LineNr)
     Next LineNr
    Close 1
    If this is not what you mean, you could look up these API functions:

    GetPrivateProfileInt
    GetPrivateProfileString
    GetProfileInt
    GetProfileString
    WritePrivateProfileString
    WriteProfileString

  10. #10
    Addicted Member
    Join Date
    Aug 2000
    Posts
    208
    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.
    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.


    hope this help, if you need a code example, drop me an e-mail at disco_boul****@email.com

    see ya

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width