Save from listbox to .txt file and load from it on

2019-08-28 22:57发布

问题:

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.

Something like this

http://pokit.org/get/img/fade9475ab42b1eaaf1b25320aed5a2d.jpg

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 ??

Edit: 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

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

回答1:

This will read each single line in a textfile and add it to a List String

Edit:

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

Note: Add this as an import first for Regex

Imports System.Text.RegularExpressions

EDIT2:

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

Assuming your listbox and textbox contains 6 different items and textlines



标签: vb6 listbox