Results 1 to 28 of 28

Thread: Remember The Details [FINALLY RESOLVED!]

  1. #1

    Thread Starter
    Supreme User Madboy's Avatar
    Join Date
    Oct 2003
    Location
    England
    Posts
    3,253

    Thumbs up Remember The Details [FINALLY RESOLVED!]

    THANKS
    Last edited by Madboy; Dec 11th, 2003 at 11:43 AM.

  2. #2
    Hyperactive Member
    Join Date
    Apr 2002
    Location
    UK
    Posts
    506
    Have you done what you stated in that post, or is that what you want to achieve?

    Or should I follow the post title and think that you want to save the values fo your text\list boxes?

    -adehh

  3. #3

    Thread Starter
    Supreme User Madboy's Avatar
    Join Date
    Oct 2003
    Location
    England
    Posts
    3,253
    Incase you didnt see my edited post above, yeah i do want to read and write the list items from the listbox.

    THANKS!

  4. #4

  5. #5

    Thread Starter
    Supreme User Madboy's Avatar
    Join Date
    Oct 2003
    Location
    England
    Posts
    3,253
    Thats exactly it marty,

    THANKS!

    Just need to know how to save to text file and laod items, thanks

  6. #6
    Fanatic Member
    Join Date
    Sep 2000
    Posts
    770
    I made it just for you


    Code:
        '-saving to text file
    
        Dim i As Integer
        Dim strBuffer As String
         
         For i = 0 To List1.ListCount - 1
            If i <> List1.ListCount - 1 Then
                strBuffer = strBuffer & List1.List(i) & ","
            Else
                strBuffer = strBuffer & List1.List(i)
            End If
        Next i
            
        Open App.Path & "\data.txt" For Output As #1
            Print #1, strBuffer
        Close #1
    Code:
        '-read from file
    
        Dim i As Integer
        Dim strBuffer As String
        Dim ListData() As String
         
        Open App.Path & "\data.txt" For Input As #1
            strBuffer = Input(LOF(1), 1)
        Close #1
        
        ListData = Split(strBuffer, ",", 1)
        
        For i = 0 To UBound(ListData)
            List1.AddItem ListData(i)
        Next i

  7. #7
    Hyperactive Member
    Join Date
    Apr 2002
    Location
    UK
    Posts
    506
    Try this:
    VB Code:
    1. Private Sub Form_Load()
    2.   If (Dir$("c:\test.txt") <> "") Then
    3.     Dim sLine As String, lFile As Long
    4.    
    5.     lFile = FreeFile
    6.     Open "c:\test.txt" For Input As #lFile
    7.       Do Until EOF(lFile)
    8.         Line Input #lFile, sLine
    9.         List1.AddItem sLine
    10.       Loop
    11.     Close #lFile
    12.   End If
    13. End Sub
    14.  
    15. Private Sub Form_Unload(Cancel As Integer)
    16.   Dim lFile As Long
    17.  
    18.   lFile = FreeFile
    19.   Open "c:\test.txt" For Output As #lFile
    20.     Dim l As Long
    21.    
    22.     For l = 0 To (List1.ListCount - 1)
    23.       Print #lFile, List1.List(l)
    24.     Next
    25.   Close #lFile
    26. End Sub
    You'd probably want to change it a tad so it suits your prog.

    -adehh

  8. #8

    Thread Starter
    Supreme User Madboy's Avatar
    Join Date
    Oct 2003
    Location
    England
    Posts
    3,253
    Thanks nkad, how do you open it again

  9. #9

    Thread Starter
    Supreme User Madboy's Avatar
    Join Date
    Oct 2003
    Location
    England
    Posts
    3,253
    Ok nkad, i got it working, but it puts to list items on the same line. How do i fix that?

  10. #10
    Fanatic Member
    Join Date
    Sep 2000
    Posts
    770
    Do a List1.Clear before you load it. This will clear the existing list..

    Glad i could help

    EDITED:

    but it puts to list items on the same line.
    Uh, do you mean in the file? Yeah, its comma delimited.

  11. #11

    Thread Starter
    Supreme User Madboy's Avatar
    Join Date
    Oct 2003
    Location
    England
    Posts
    3,253
    ok thanks, but when i load it the list box shows as (see attached pic), but the text in the text file shows as:

    test
    test2
    test3

    I modified the code (i think i should have included CRLF) here is the modified code:

    VB Code:
    1. Private Sub Form_Load()
    2. ReDim strPasswordCodes(0)
    3. lstMain.Clear
    4. Dim i As Integer
    5. Dim strBuffer As String
    6. Dim ListData() As String
    7.      
    8. Open App.Path & "\data.txt" For Input As #1
    9. strBuffer = Input(LOF(1), 1)
    10. Close #1
    11.    
    12. ListData = Split(strBuffer, vbCrLf, 1)
    13.    
    14. For i = 0 To UBound(ListData)
    15.         lstMain.AddItem ListData(i)
    16.     Next i
    17. End Sub

    and here (i used a label as a command button instead of Form Unload)

    VB Code:
    1. Private Sub lblOK_Click()
    2. Dim i As Integer
    3. Dim strBuffer As String
    4.      
    5.      For i = 0 To lstMain.ListCount - 1
    6.         If i <> lstMain.ListCount - 1 Then
    7.             strBuffer = strBuffer & lstMain.List(i) & vbCrLf
    8.         Else
    9.             strBuffer = strBuffer & lstMain.List(i)
    10.         End If
    11.     Next i
    12.        
    13.     Open App.Path & "\data.txt" For Output As #1
    14.         Print #1, strBuffer
    15.     Close #1
    16. End Sub

    and here is my attached picture, hope it is clear. Thanks for helping
    Attached Images Attached Images  

  12. #12

    Thread Starter
    Supreme User Madboy's Avatar
    Join Date
    Oct 2003
    Location
    England
    Posts
    3,253
    Ok i used your code instead adzzzz, now what do i do for when i click the listbox? How do i get the correct password?

  13. #13
    Fanatic Member
    Join Date
    Sep 2000
    Posts
    770
    Sigh...

    I already told you how to get the selected item of a list box many times..

    your_password = List1.ListIndex

  14. #14
    Fanatic Member
    Join Date
    Sep 2000
    Posts
    770
    You put that in the List1_Click event

  15. #15

    Thread Starter
    Supreme User Madboy's Avatar
    Join Date
    Oct 2003
    Location
    England
    Posts
    3,253
    Yeah, but using the code from marty's example:

    txtPassword.Text = strPasswordCodes(lstMain.ListIndex)

    I get the error, subscript out of range

  16. #16

    Thread Starter
    Supreme User Madboy's Avatar
    Join Date
    Oct 2003
    Location
    England
    Posts
    3,253
    So i take it i also need to save the strPasswordCodes to a text file ,

    Wheres marty when you need him

  17. #17

  18. #18

    Thread Starter
    Supreme User Madboy's Avatar
    Join Date
    Oct 2003
    Location
    England
    Posts
    3,253
    Ok, its just im getting mixed up. Your code was great, then i got a different code to read and write the list box from a file. But i just need to know how to find out the right code when i click the list box. Because im confused having used your code, and someone elses, i tried yours for the list click but it errors saying "Subscript out of range" or something. So im pretty confused now

    Anyway i was hoping you could help further more, seeming as your code has been the most effecient so far

    THANKS MARTY!

  19. #19
    Fanatic Member
    Join Date
    Sep 2000
    Posts
    770
    No love for nkad

  20. #20

    Thread Starter
    Supreme User Madboy's Avatar
    Join Date
    Oct 2003
    Location
    England
    Posts
    3,253
    Thanks nkad too, im just confused again, code flying all over the place here. Please help before i go to bed

  21. #21
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431
    Originally posted by Madboy
    Ok, its just im getting mixed up. Your code was great, then i got a different code to read and write the list box from a file. But i just need to know how to find out the right code when i click the list box. Because im confused having used your code, and someone elses, i tried yours for the list click but it errors saying "Subscript out of range" or something. So im pretty confused now

    Anyway i was hoping you could help further more, seeming as your code has been the most effecient so far

    THANKS MARTY!
    Basically what you need to do is to save the codes in an array that is in the same order as the items in List1. Then when List1 is clicked all you need to do is to use the ListIndex of List1 as the index of the array and display the array value in Text1.

  22. #22

    Thread Starter
    Supreme User Madboy's Avatar
    Join Date
    Oct 2003
    Location
    England
    Posts
    3,253
    Here is a commented example of what i mean, maybe it makes slightly more sense.

    THANKS
    Attached Files Attached Files

  23. #23

    Thread Starter
    Supreme User Madboy's Avatar
    Join Date
    Oct 2003
    Location
    England
    Posts
    3,253
    How marty im no good arrays.

    Ok, i dont expect anybody juist to give me code, id rather get help, otherwise id never learn. I just need more into depth help

    THANKS

  24. #24
    Hyperactive Member
    Join Date
    Apr 2002
    Location
    UK
    Posts
    506
    I'll have a shot at trying to inform you, rather than just supplying code..

    First off you've really not got much need to worry about the array because it should all fit in quite easily to the code you already have. The indexes for the listitems should match to the array therefore lstMain.List(0) would match up to the password in strPasswordCode(0).

    All you need to do is save the password AND the password name to your data file when you unload the form, use a divider to dictate between the password and name, then use the left and mid functions as you extract the data line-by-line from the file. So in the end you would be extracting the data file like this:
    Code:
    password name       password
    my hotmail password,letmein
    With that method, you'd have to stop the user from entering a comma in their passwords as the text before the comma would be the name and the text after the password. You don't need to prevent comma entry for the name because you could use the instrrev function for finding the comma.

    Saving the data is even easier, just loop through the listitems as you are now but add the password to the end of the line with a comma between the two:
    VB Code:
    1. Print #lFile, lstMain.List(l) & Chr$(44) & Passwords(l)
    If you don't know about the chr function it just returns a chracter relating to the specified ascii code and I used it here just because I don't like placing single chracters as ",".. don't ask.

    Another method of saving the data would be as two data files, one for passwords and one for the password names. I'm not sure how much trouble would arise due to the lines not referencing to each other correctly though so I'd go for the other method.

    Have you thought about encrypting the password data as you save it to prevent people viewing it or will your program not be implemented in such a way that this would matter?

    Your code for the on click event of the listbox is perfect, it was just trying to access a dimension of an array that didn't exist because the code for loading the data didn't compensate for both the password and the password name.

    If I've completely confused you with anything there just say so, or if you want the code I'll zippit up for you as I've put something together.

    -adehh

  25. #25
    Fanatic Member
    Join Date
    Sep 2000
    Posts
    770
    Alright MadBoy, I updated your code and now it works, and makes more sense!

    Have fun
    Attached Files Attached Files

  26. #26

    Thread Starter
    Supreme User Madboy's Avatar
    Join Date
    Oct 2003
    Location
    England
    Posts
    3,253
    Sweet, thanks nkad

  27. #27

  28. #28

    Thread Starter
    Supreme User Madboy's Avatar
    Join Date
    Oct 2003
    Location
    England
    Posts
    3,253
    I sent one back

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