从列表框保存到.txt文件,并在启动时从中加载 [英] Save from listbox to .txt file and load from it on start
问题描述
如果我有名称为lstPerioda
的listox和名称为txtDescription
的文本框.我想为列表中的每个项目选择并保存/加载txtdescription
中的不同文本.
If i have listox with name lstPerioda
and textbox with name txtDescription
. I want to select and save/load different text in txtdescription
for each item in list.
类似这样的东西
http://pokit.org/get/img/fade9475ab42b1eaaf1b25320aed5a2d.jpg
这些是我将要做的笔记.所有这些都应保存到某种文档中.我曾经考虑过.txt
文件,或者也许是database
您怎么看???
Those are some kind of notes what i will do. All this should be saved to some kind of document. I was think about .txt
file or maybe database
what do you think ??
我可以将列表框中的值写入.txt
文件,然后再次加载
I can write the values from listbox to .txt
file and load it again
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
但是我如何制作不同的文本,每次我启动应用程序时,该文本将保存到某些.c6文件中并从中读取.也可以更新.
But how can i make different text which will be saved to some .txt
file and read from it each time i start application. Also be available for update.
基本上,如果我写的描述是今天是星期一",我想将其保存到文件中,每次我从列表框中选择星期一"时,它都会向我显示该描述
So basicly if i write to description that Today is Monday i want to save that to file and each time i select monday from listbox it show's me that description
推荐答案
这将读取文本文件中的每一行并将其添加到列表字符串中
This will read each single line in a textfile and add it to a List String
Dim lists As New List(Of String)
Private Sub Main()
Dim FSO As FileSystemObject
Dim TS As TextStream
Dim Final As String
Set FSO = New FileSystemObject
Set TS = FSO.OpenTextFile("C:\text.txt", ForReading)
Final = TS.ReadAll
Dim pattern As String = "\r\n|\r|\n"
Dim result() As String = Regex.Split(Final, pattern)
For Each s As String In result
lists.Add(s)
Next
End Sub
注意:首先将其添加为Regex的导入
Note: Add this as an import first for Regex
Imports System.Text.RegularExpressions
在列表框中选择的项目如下您可以显示文本:
You can display your text when listbox items selected as following:
Private Sub ListBox_SelectedIndexChanged()
Select ListBox1.SelectedIndex
Case 0
TextBox1.Text = lists.Item(0)
Case 1
TextBox1.Text = lists.Item(1)
Case 2
TextBox1.Text = lists.Item(2)
Case 3
TextBox1.Text = lists.Item(3)
Case 4
TextBox1.Text = lists.Item(4)
Case 5
TextBox1.Text = lists.Item(5)
End Select
End Sub
假设您的列表框和文本框包含6个不同的项目和文本行
Assuming your listbox and textbox contains 6 different items and textlines
这篇关于从列表框保存到.txt文件,并在启动时从中加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!