从文件加载到列表框?

发布于 2024-11-14 09:18:11 字数 280 浏览 2 评论 0原文

因此,在我的程序中,我试图做到这一点,以便当您按下按钮时,它会打开一个“选择文件”弹出窗口(用户可以在其中选择一个文本文件),然后在用户选择它之后,程序将自动加载该文件的每一行文本文件放入列表框中。

但我一直在研究它,我唯一能找到的是文件>打开的东西。所以我让它在按下按钮时打开一个“打开”对话框有多冷

,因为我无法在打开的对话框中找到任何内容,所以我没有在将其每一行加载到列表框中时寻找任何内容,所以如果有人想帮助我,那就太好了。

我没有任何代码可以向您展示,因为我的程序的其余部分与此无关

So in my program i am trying to make it so that when you press a button it opens a "choose file" popup(where the user can choose a text file) then after the user chooses it the program will automatically load each line of the textfile into a listbox.

But i have been researching it and the only thing i have been able to find is a file>open thing. So how cold i make it open a "open" dialogue on the press of a button

and since i haven't been able to find anything on the open dialogue i haven't looked for anything on the loading each line of it into a listbox, so if anyone wants ot help me with that it would be great.

and i do not have any code to show you as the rest of my program has no relevance to this

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

青朷 2024-11-21 09:18:11
    Using FD As New OpenFileDialog()
        FD.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"
        If FD.ShowDialog = Windows.Forms.DialogResult.OK Then
            Listbox1.Items.Clear
            ListBox1.Items.AddRange(IO.File.ReadAllLines(FD.FileName))
        End If
    End Using

编辑:回答评论:

如果您可以使用 LINQ,那么只需一行代码即可从列表框中读取所有行并将其写入文件:

使用 SaveFileDialog 和 LINQ 保存< /strong>

    Using FD As New SaveFileDialog()
        FD.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"
        If FD.ShowDialog = Windows.Forms.DialogResult.OK Then
            IO.File.WriteAllLines(fd.filename, (From p As String In ListBox1.Items Select p).ToArray)
        End If
    End Using

如果您无法使用 LINQ,那么您可以这样做:

使用 SaveFileDialog 和 FOR/EACH 进行保存

     Using FD As New SaveFileDialog()
        FD.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"
        If FD.ShowDialog = Windows.Forms.DialogResult.OK Then
            Dim FileContent As String = ""
            For Each i As String In ListBox1.Items
                FileContent &= i & vbCrLf
            Next
            IO.File.WriteAllText(FD.FileName, FileContent)
        End If
    End Using 
    Using FD As New OpenFileDialog()
        FD.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"
        If FD.ShowDialog = Windows.Forms.DialogResult.OK Then
            Listbox1.Items.Clear
            ListBox1.Items.AddRange(IO.File.ReadAllLines(FD.FileName))
        End If
    End Using

EDIT: Answer on the comment:

If you can use LINQ, then its a one row of code to read all lines from the listbox and write it to a file:

Save using SaveFileDialog and LINQ

    Using FD As New SaveFileDialog()
        FD.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"
        If FD.ShowDialog = Windows.Forms.DialogResult.OK Then
            IO.File.WriteAllLines(fd.filename, (From p As String In ListBox1.Items Select p).ToArray)
        End If
    End Using

If you can't use LINQ, then you can do this instead:

Save using SaveFileDialog and FOR/EACH

     Using FD As New SaveFileDialog()
        FD.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"
        If FD.ShowDialog = Windows.Forms.DialogResult.OK Then
            Dim FileContent As String = ""
            For Each i As String In ListBox1.Items
                FileContent &= i & vbCrLf
            Next
            IO.File.WriteAllText(FD.FileName, FileContent)
        End If
    End Using 
明媚如初 2024-11-21 09:18:11

基本上,这里有几个部分。首先,您要创建一个“打开文件”对话框,提示用户文件所在位置。操作方法如下:

http://www.homeandlearn.co.uk/net/ nets4p6.html

接下来,您想要将文本文件逐行读取到列表框中。以下是读取文本文件的方式(您需要修改代码以将行添加到列表框中,而不是执行 Console.WriteLine:

http://msdn.microsoft.com/en-us/library/db5x7c0d.aspx

Basically, there are a couple parts here. First, you want to create an Open File Dialog box that prompts the user for where the file is. Here is how you do that:

http://www.homeandlearn.co.uk/net/nets4p6.html

Next, you want to read the text file in line by line into your listbox. Here is how you read your text file (you will need to modify the code to have it add the lines to the listbox instead of doing a Console.WriteLine:

http://msdn.microsoft.com/en-us/library/db5x7c0d.aspx

谁对谁错谁最难过 2024-11-21 09:18:11

来自 MSDN 的 OpenFileDialog

   Private Sub button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim myStream As Stream = Nothing
        Dim openFileDialog1 As New OpenFileDialog()

        openFileDialog1.InitialDirectory = "c:\"
        openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"
        openFileDialog1.FilterIndex = 2
        openFileDialog1.RestoreDirectory = True

        If openFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
            Try
                myStream = openFileDialog1.OpenFile()
                If (myStream IsNot Nothing) Then
                    ' Insert code to read the stream here.
                End If
            Catch Ex As Exception
                MessageBox.Show("Cannot read file from disk. Original error: " & Ex.Message)
            Finally
                ' Check this again, since we need to make sure we didn't throw an exception on open.
                If (myStream IsNot Nothing) Then
                    myStream.Close()
                End If
            End Try
        End If
    End Sub

如您所见注释,您可以在用户打开文件后读取流。然后,您可以使用 StreamReader。这将为您提供用户选择的文件中的数据。根据您的需要,您可以解析该数据并将其添加到列表框中。

OpenFileDialog from MSDN:

   Private Sub button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim myStream As Stream = Nothing
        Dim openFileDialog1 As New OpenFileDialog()

        openFileDialog1.InitialDirectory = "c:\"
        openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"
        openFileDialog1.FilterIndex = 2
        openFileDialog1.RestoreDirectory = True

        If openFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
            Try
                myStream = openFileDialog1.OpenFile()
                If (myStream IsNot Nothing) Then
                    ' Insert code to read the stream here.
                End If
            Catch Ex As Exception
                MessageBox.Show("Cannot read file from disk. Original error: " & Ex.Message)
            Finally
                ' Check this again, since we need to make sure we didn't throw an exception on open.
                If (myStream IsNot Nothing) Then
                    myStream.Close()
                End If
            End Try
        End If
    End Sub

As you can see from the comments, you can read a stream after the user has opened the file. You can then read this stream using, for example, a StreamReader. That will give you the data within the file the user chose. Depending on what you want, you can then parse that data and add it to your listbox.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文