Results 1 to 14 of 14

Thread: ini file and listbox

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jul 2005
    Posts
    158

    ini file and listbox

    i have 3 textboxes and a listbox, i can write to an ini file with the textbox information. Now i need to know how i can write the the same ini file with the listbox data. So in the end, on the ini file, there should be the 3 textboxes' data and after that data, there should be the listbox data. Then i would also need to know how to display all the info when the program is ran again.

    thanx all

  2. #2
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: ini file and listbox

    Write the data to the ini file

    VB Code:
    1. Dim lngIndex As Long
    2. Dim strData As String
    3.  
    4. For lngIndex = 0 To List1.ListCount - 1
    5.     ' If a semicolon can be in the data then use a different character or characters
    6.     strData = strData & ";" & List1.List(lngIndex)
    7. Next
    8.  
    9. ' get rid of the 1st semicolon
    10. strData = Mid$(strData, 2)

    Get it back
    VB Code:
    1. Dim strParts() As String
    2. Dim lngIndex As Long
    3. Dim strData As String
    4.  
    5. strParts = Split(strData, ";")
    6. For lngIndex = 0 To UBound(strParts)
    7.     List1.AddItem strParts(lngIndex)
    8. Next

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jul 2005
    Posts
    158

    Re: ini file and listbox

    i got it working and there are no errors, but, it doesnt seem to want to put all the data in the listbox, sumtimes 3 lines, sumtimes 2.
    Last edited by Ch4s3t0ph3r; Dec 10th, 2006 at 01:13 PM.

  4. #4

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Jul 2005
    Posts
    158

    Re: ini file and listbox

    VB Code:
    1. Sub ReadIni()
    2. Dim strParts() As String
    3. Dim lngIndex As Long
    4. Dim strData As String
    5.  
    6. inifile = "C:\file\" & Text18.Text & ".ini"
    7. strData = GetIni("Items", "#18")
    8.  
    9. strParts = Split(strData, ";")
    10. For lngIndex = 0 To UBound(strParts)
    11.     List1.AddItem strParts(lngIndex)
    12. Next
    13. End Sub

    VB Code:
    1. Private Sub Command1_Click()
    2. ReadIni
    3. End Sub
    Last edited by Ch4s3t0ph3r; Dec 10th, 2006 at 01:13 PM.

  6. #6
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: ini file and listbox

    What about to write the data?

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Jul 2005
    Posts
    158

    Re: ini file and listbox

    VB Code:
    1. Private Sub mnuSave_Click()
    2. Open "C:\file\" & Text27.Text & ".ini" For Output As #1
    3. Dim lngIndex As Long
    4. Dim strData As String
    5.  
    6. For lngIndex = 0 To List1.ListCount - 1
    7.     ' If a semicolon can be in the data then use a different character or characters
    8.     strData = strData & ";" & List1.List(lngIndex)
    9. Next
    10.  
    11. ' get rid of the 1st semicolon
    12. strData = Mid$(strData, 2)
    13.  
    14. Print #1, "[Items]"
    15. Print #1, "#18 ="; strData
    16. Close #1
    17.  
    18. End Sub
    and there are no other semicolons in the data im using
    Last edited by Ch4s3t0ph3r; Dec 10th, 2006 at 01:13 PM.

  8. #8

  9. #9
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: ini file and listbox

    Use this to get rid of the last semicolon:

    VB Code:
    1. strData = Left(strData, Len(strData) - 1)

    The MID() statement is deleting most of the data
    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Form_Load()
    4.   Dim x As Integer, str$
    5.   For x = 0 To 3
    6.     List1.AddItem x * 7
    7.   Next x
    8.   For x = 0 To List1.ListCount - 1
    9.     str = str & List1.List(x) & ";"
    10.   Next x
    11.   MsgBox str
    12.   str = Left(str, Len(str) - 1)
    13.   MsgBox str
    14. End Sub
    Last edited by dglienna; Dec 4th, 2005 at 12:07 AM.

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    Jul 2005
    Posts
    158

    Re: ini file and listbox

    Quote Originally Posted by dglienna
    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Form_Load()
    4.   Dim x As Integer, str$
    5.   For x = 0 To 3
    6.     List1.AddItem x * 7
    7.   Next x
    8.   For x = 0 To List1.ListCount - 1
    9.     str = str & List1.List(x) & ";"
    10.   Next x
    11.   MsgBox str
    12.   str = Left(str, Len(str) - 1)
    13.   MsgBox str
    14. End Sub
    im not understanding what that is for or where to put it, i did for forget to mention that i am taking info from one form and this is saved to the ini, then it can only be viewable on form2 which has the same setup and it all works but the only thing is that the listbox doesnt recieve all the info that is in the ini file

  11. #11
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: ini file and listbox

    Quote Originally Posted by dglienna
    Use this to get rid of the last semicolon:

    VB Code:
    1. strData = Left(strData, Len(strData) - 1)

    The MID() statement is deleting most of the data
    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Form_Load()
    4.   Dim x As Integer, str$
    5.   For x = 0 To 3
    6.     List1.AddItem x * 7
    7.   Next x
    8.   For x = 0 To List1.ListCount - 1
    9.     str = str & List1.List(x) & ";"
    10.   Next x
    11.   MsgBox str
    12.   str = Left(str, Len(str) - 1)
    13.   MsgBox str
    14. End Sub
    In my code and the code that Ch4s3t0ph3r showed that he was using the "extra" semicolon is placed first, not last, and the Mid statement grabs everything except the first character (the "extra" semicolon).

  12. #12
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: ini file and listbox

    That's just the sample that I whipped up, which cut off a lot of the information when I used the MID() statement that you had, so I changed it around, then added the correction for your form.

    If you paste that code into a new project, and run it, you will see that it works correctly. All items are displayed.

    You can use Form2.Text1.text from Form1 to get the value.

  13. #13
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: ini file and listbox

    Quote Originally Posted by dglienna
    That's just the sample that I whipped up, which cut off a lot of the information when I used the MID() statement that you had, so I changed it around, then added the correction for your form.

    If you paste that code into a new project, and run it, you will see that it works correctly. All items are displayed.

    You can use Form2.Text1.text from Form1 to get the value.
    So does mine if you had bothered to try it.

  14. #14
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: ini file and listbox

    I typed it in wrong. When I tried the mid statement, it messed up my data, so I used left. I added the ";" to the end, and removed it. You added ";" at the beginning of the data, and the Mid() would have worked.

    I assumed that Ch4s3t0ph3r did try it, though.

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