THANKS:)
Printable View
THANKS:)
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
Incase you didnt see my edited post above, yeah i do want to read and write the list items from the listbox.
THANKS!
Something like this?
Thats exactly it marty,
THANKS!;) :wave:
Just need to know how to save to text file and laod items, thanks:)
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
Try this:
You'd probably want to change it a tad so it suits your prog.VB Code:
Private Sub Form_Load() If (Dir$("c:\test.txt") <> "") Then Dim sLine As String, lFile As Long lFile = FreeFile Open "c:\test.txt" For Input As #lFile Do Until EOF(lFile) Line Input #lFile, sLine List1.AddItem sLine Loop Close #lFile End If End Sub Private Sub Form_Unload(Cancel As Integer) Dim lFile As Long lFile = FreeFile Open "c:\test.txt" For Output As #lFile Dim l As Long For l = 0 To (List1.ListCount - 1) Print #lFile, List1.List(l) Next Close #lFile End Sub
-adehh
Thanks nkad, how do you open it again:D :blush:
Ok nkad, i got it working, but it puts to list items on the same line. How do i fix that?
Do a List1.Clear before you load it. This will clear the existing list..
Glad i could help :cool:
EDITED:
Uh, do you mean in the file? Yeah, its comma delimited.Quote:
but it puts to list items on the same line.
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:
Private Sub Form_Load() ReDim strPasswordCodes(0) lstMain.Clear 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, vbCrLf, 1) For i = 0 To UBound(ListData) lstMain.AddItem ListData(i) Next i End Sub
and here (i used a label as a command button instead of Form Unload)
VB Code:
Private Sub lblOK_Click() Dim i As Integer Dim strBuffer As String For i = 0 To lstMain.ListCount - 1 If i <> lstMain.ListCount - 1 Then strBuffer = strBuffer & lstMain.List(i) & vbCrLf Else strBuffer = strBuffer & lstMain.List(i) End If Next i Open App.Path & "\data.txt" For Output As #1 Print #1, strBuffer Close #1 End Sub
and here is my attached picture, hope it is clear. Thanks for helping;)
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?
Sigh...
I already told you how to get the selected item of a list box many times..
your_password = List1.ListIndex
You put that in the List1_Click event
Yeah, but using the code from marty's example:
txtPassword.Text = strPasswordCodes(lstMain.ListIndex)
I get the error, subscript out of range
So i take it i also need to save the strPasswordCodes to a text file:confused: ,
Wheres marty when you need him:(
Out running :)Quote:
Originally posted by Madboy
So i take it i also need to save the strPasswordCodes to a text file:confused: ,
Wheres marty when you need him:(
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:confused:
Anyway i was hoping you could help further more, seeming as your code has been the most effecient so far;)
THANKS MARTY!:wave:
No love for nkad :(
Thanks nkad too, im just confused again, code flying all over the place here. Please help before i go to bed:o
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.Quote:
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:confused:
Anyway i was hoping you could help further more, seeming as your code has been the most effecient so far;)
THANKS MARTY!:wave:
Here is a commented example of what i mean, maybe it makes slightly more sense.
THANKS:)
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
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:
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.Code:password name password
my hotmail password,letmein
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.VB Code:
Print #lFile, lstMain.List(l) & Chr$(44) & Passwords(l)
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
Alright MadBoy, I updated your code and now it works, and makes more sense!
Have fun :D
Sweet, thanks nkad;)
Madboy, I sent you a PM that might be helpful.
I sent one back;)