Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Working with .Ini file

Status
Not open for further replies.

theCroat

Technical User
Sep 27, 2006
20
HR
Hello! I have question, if anyone has a little bit of time ro spend on this project.. I have Combo Box ,Label1 and Teams.ini file.
Inside Teams.ini is list of teams and id's example:

Arsenal,25
Koln,33
Juventus,22
...

what i want is when i click on combo box that it shows team names as in example [Arsenal, Koln, Juventus] and in label1.caption team id's like on example: [25,33,22]

Thank you alot!
 
Hi,

thread222-1279342 has some very useful code and links provided by Golom for working with INI files.

Hope this helps

HarleyQuinn
---------------------------------
Get the most out of Tek-Tips, read FAQ222-2244 before posting.
 
What do you mean? Did it not help at all? Did you already know how to read from an INI file? What part of your original question are you having trouble with?

HarleyQuinn
---------------------------------
Get the most out of Tek-Tips, read FAQ222-2244 before posting.
 
Is the whole file just pairs of team names and ID's, separated by a comma?
If so, it's really just a plain old CSV file. You could just read in the file one line at a time, and use the Split function to divide the team names and IDs.
 
Ini files have a specific format that you need to follow for the WritePrivateProfileString and GetPrivateProfileString API functions to work properly. Your file would need to be in the form
Code:
[TEAMS]         [red]<--- The section needs a title enclosed in brackets[/red]
Arsenal=25      [red]<--- Each Item is in the form 'Name=Value'[/red]
Koln=33
Juventus=22
Then, assuming you have two list boxes, List1 and List2
Code:
Private Sub Command1_Click()
Const Ifile As String = "C:\Teams.INI"
Dim SectionParts()              As String
Dim n                           As Integer

SectionParts = Split(ProfileGetSection("TEAMS", Ifile), Chr(0))
For n = 0 To UBound(SectionParts)
    List1.AddItem SectionParts(n)
    List2.AddItem ProfileGetItem("TEAMS", SectionParts(n), Ifile)
Next
End Sub
Loads the list boxes with
[tt]
List1 List2

Arsenal 25
Koln 33
Juventus 22
[/tt]

Check out the thread that HarleyQuinn referenced for definitions of the functions that are used here.
 
OOps ... I guess ProfileGetSection isn't in Randy's code. Here it is
Code:
'----------------------------------------------------------
' Procedure : ProfileGetSection
' Purpose   : Retrieves a Chr(0) delimited string of variables within a Section
'----------------------------------------------------------

Public Function ProfileGetSection(lpSectionName As String, _
                                  IniFile As String) As String


    Dim Success                     As Long
    Dim nSize                       As Long
    Dim Ret                         As String

    'call the API with the parameters passed.
    'The return value is the length of the string
    'in ret, including the terminating null. If a
    'default value was passed, and the section or
    'key name are not in the file, that value is
    'returned. If no default value was passed (""),
    'then success will = 0 if not found.

    'Pad a string large enough to hold the data.
    Ret = Space$(4096)
    nSize = Len(Ret)
    Success = GetPrivateProfileString(lpSectionName, _
                                      vbNullString, _
                                      vbNullString, _
                                      Ret, _
                                      nSize, _
                                      IniFile)

    If Success > 0 Then
        ProfileGetSection = Left$(Ret, Success - 1) ' remove the trailing Chr(0)
    Else
        ProfileGetSection = ""
    End If

End Function
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top