'-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
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
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.
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:
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.