通过列表框请求从 .txt 读取文本 [英] Read text from .txt by the listbox request
问题描述
我想做这样的东西
示例:像刨床这样的东西,我写了我需要在特定日期做的事情,当我在列表框中选择时,它会告诉我我需要做什么.
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.
因此,因为它会记住需要保存在某种 .txt 或数据库中的数据.
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.
我需要在一周内每天制作一个新文件,比如 7 个文件,还是有更简单的方法??
Do i need to make a new file for each day in a week like 7 files or there is a easier way ??
推荐答案
没有必要为每个列表框项目制作单独的文件.
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
代码的设计使其可以与手动插入planner.txt"文件的其他项目一起使用.如果这样做,请确保在项目名称后面加上逗号 ( , ).
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.
这篇关于通过列表框请求从 .txt 读取文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!