问题
I want to make something like this
Example : Something like planer i write things i need to do for specific days and when i choose in a listbox day it show's me what i need to do.
So because it will remember data it need's to be saved on some kind of .txt or database .
I can add/load items from listbox like this
Private Sub Command1_Click()
Open "Listbox.txt" For Output As #1
For i = 0 To list1.ListCount - 1
Print #1, list1.List(i)
Next
Close
End Sub
Private Sub Form_Load()
list1.AddItem "Monday"
list1.AddItem "Tuesday"
list1.AddItem "Wednesday"
list1.AddItem "Thursday"
list1.AddItem "Friday"
list1.AddItem "Saturday"
list1.AddItem "Sunday"
End Sub
But the main problem is text save. How can i save the text from a textbox for a specific day or edit it . So when i click on Monday it show's me the custom text i entered for monday.
Do i need to make a new file for each day in a week like 7 files or there is a easier way ??
回答1:
It's not necessary to make separate files for each listbox item.
Here's my sample project; I made a couple of changes to your form layout:
When an item is clicked in the list at the left, the text field will provide the text associated with the item. Update the item by typing something into / changing the same field.
Option Explicit
Private FileStr As String, StrArr() As String
Private Sub cmdSave_Click()
Dim I As Long
Open FileStr For Output As #1
For I = 0 To lbxItems.ListCount - 1
Print #1, lbxItems.List(I) & "," & StrArr(I)
Next I
Close #1
End Sub
Private Sub cmdUpdate_Click()
StrArr(lbxItems.ListIndex) = txtDescript
End Sub
Private Sub Form_Load()
Dim I As Long, J As Long
Dim TempStr As String
FileStr = App.Path & "\planner.txt"
Open FileStr For Input As #1
Do Until EOF(1)
Line Input #1, TempStr
J = InStr(TempStr, ",")
lbxItems.AddItem Left$(TempStr, J - 1)
TempStr = Mid$(TempStr, J + 1)
ReDim Preserve StrArr(I)
StrArr(I) = TempStr
I = I + 1
Loop
Close #1
End Sub
Private Sub lbxItems_Click()
txtDescript = StrArr(lbxItems.ListIndex)
End Sub
The code is designed so that it can be used with other items that are inserted into the "planner.txt" file manually. If you do this, make sure that you follow the item name with a comma ( , ).
If you copy and paste the code straight into your form code module as is, make sure to update the control names as necessary.
来源:https://stackoverflow.com/questions/25686012/read-text-from-txt-by-the-listbox-request