简体   繁体   English

VB如何将列表框项目写入文本文件

[英]VB how to write listbox items to a text file

Okay so I'm creating a personal to-do list program inside of VB(Using visual studio) and I'm having trouble implementing the function where the items inside of the listbox get added to a textfile. 好的,所以我要在VB(使用Visual Studio)中创建一个个人待办事项列表程序,并且在实现将列表框中的项添加到文本文件的功能时遇到了麻烦。

Here is the code I have tried: 这是我尝试过的代码:

Private Sub btnAddItem_Click(sender As Object, e As EventArgs) Handles btnAddItem.Click 私有Sub btnAddItem_Click(作为对象发送,作为EventArgs发送)处理btnAddItem.Click

    ''Adds item to the listbox.
    list.Items.Add(txtItem.Text)

    txtItem.Clear() ''Clears the textbox after adding item.
    txtItem.Select() ''Sets the cursor onto the textbox after adding item.

    Dim file As System.IO.StreamWriter
    file = My.Computer.FileSystem.OpenTextFileWriter("Test.txt", True)
    file.WriteLine(list.Items)
    file.Close()

End Sub

I have also tried; 我也尝试过; file.WriteLine(list.Items.Text) however that is not an option inside of that statement. file.WriteLine(list.Items.Text),但这不是该语句内的选项。

The button will not be how the list is saved but I'm just trying to get the test file to write properly first. 该按钮将不会是列表的保存方式,但我只是想让测试文件首先正确编写。 The test file is created however if I type in: test 1 test 2 test 3 Into the listbox, the Test.txt file simply shows: 但是,如果输入以下内容,则会创建测试文件:test 1 test 2 test 3在列表框中,Test.txt文件仅显示:

System.Windows.Forms.ListBox+ObjectCollection
System.Windows.Forms.ListBox+ObjectCollection
System.Windows.Forms.ListBox+ObjectCollection

Any help would be greatly appreciated! 任何帮助将不胜感激!

After you have added all the items to the list box then you need to address each item individually in a loop. 将所有项目添加到列表框中后,您需要循环处理每个项目。

Private Sub WriteListItems()
        Dim lst As String = ""
        For Each item As String In ListBox1.Items
            lst &= item & vbCrLf
        Next
        Debug.Print(lst)
End Sub

You need to loop each item in your ListBox and add them to your Test.txt . 您需要循环ListBox每个项目并将它们添加到Test.txt you can have it in separate method. 您可以使用其他方法。

Private Sub putItem()
    For Each i In list.Items
        Dim file As System.IO.StreamWriter
        file = My.Computer.FileSystem.OpenTextFileWriter("Test.txt", True)
        file.WriteLine(i)
        file.Close()
    Next
End Sub

If you wish to add Item in the Test.txt every clicking the button just like in your example. 如果您希望在每次单击按钮时都在Test.txt中添加Item,就像您的示例一样。 you can replace your code: file.WriteLine(list.Items) to file.WriteLine(txtItem.Text) and move txtItem.Clear() to the end of the ButtonClick() so that txtItem.Text are added to file before clearing it. 你可以取代你的代码: file.WriteLine(list.Items)file.WriteLine(txtItem.Text)和移动txtItem.Clear()到年底ButtonClick()这样txtItem.Text添加清除之前提交。

list.Items.Add(txtItem.Text)

Dim file As System.IO.StreamWriter
file = My.Computer.FileSystem.OpenTextFileWriter("Test.txt", True)
file.WriteLine(txtItem.Text)
file.Close()

txtItem.Clear() ''Clears the textbox after adding item.
txtItem.Select() ''Sets the cursor onto the textbox after adding item.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM