簡體   English   中英

在VB中使用打開文件對話框和保存文件對話框以及列表框

[英]Using open file dialog and save file dialog with a list box in VB

我需要保存某人添加到列表中的內容,然后打開一個txt文件,將其放入列表框中。 當我打開一個txt文件時,我只會得到一行代碼,而我的保存嘗試只會生成空的txt文件。 任何幫助將不勝感激。 這是我的代碼:

Imports System.IO

Public Class Form1
    Public Listed As String

    Private Sub OpenToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenToolStripMenuItem.Click
        Dim AllText As String = "", LineOfText As String = ""
        Dim StreamToDisplay As StreamReader
        OpenFileDialog1.Filter = "Text files (*.txt)}|*.txt"
        If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
            Try
                StreamToDisplay = My.Computer.FileSystem.OpenTextFileReader(OpenFileDialog1.FileName)
                Label1.Text = OpenFileDialog1.FileName
                Do Until StreamToDisplay.EndOfStream
                    LineOfText = StreamToDisplay.ReadLine()
                    'AllText = AllText & LineOfText & vbCrLf
                    lstBox.Items.Add(Listed)
                Loop
                lstBox.Items.Add(AllText).ToString()
                StreamToDisplay.Close()
                CloseToolStripMenuItem.Enabled = True
                OpenToolStripMenuItem.Enabled = False
            Catch ex As Exception
                MsgBox("An error occurred." & vbCrLf & ex.Message)
            End Try
        End If
    End Sub

    Private Sub SaveToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveToolStripMenuItem.Click
        SaveFileDialog1.Filter = "Text files (*.txt)|*.txt"
            If SaveFileDialog1.ShowDialog() = DialogResult.OK Then
                My.Computer.FileSystem.WriteAllText(SaveFileDialog1.FileName, lstBox.Items.ToString(), False)
            End If
    End Sub

    Private Sub CloseToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CloseToolStripMenuItem.Click
        lstBox.Items.Clear()
        Label1.Text = ""
        CloseToolStripMenuItem.Enabled = False
        OpenToolStripMenuItem.Enabled = True
    End Sub

    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        Dim Prompt As String = "Enter Items To Add Here"
        Listed = InputBox(Prompt)
        lstBox.Items.Add(Listed).ToString()
    End Sub

    Private Sub btnRemove_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRemove.Click
        With lstBox
            .Items.Remove(.SelectedItem)
        End With
    End Sub
End Class

這是一個簡單的示例:

  • 將項目添加到ListBox
  • 將它們保存到文件
  • 從文件加載它們並用它們填充ListBox

碼:

Imports System.IO

Public Class Form1
    Private Sub ButtonAddItem_Click(sender As Object, e As EventArgs) Handles ButtonAddItem.Click
        ListBox1.Items.Add(DateTime.Now.Ticks)
    End Sub

    Private Sub ButtonSave_Click(sender As Object, e As EventArgs) Handles ButtonSave.Click
        If SaveFileDialog1.ShowDialog() = DialogResult.OK Then
            Using writer = New StreamWriter(SaveFileDialog1.FileName)
                For Each o As Object In ListBox1.Items
                    writer.WriteLine(o)
                Next
            End Using
        End If
    End Sub

    Private Sub ButtonLoad_Click(sender As Object, e As EventArgs) Handles ButtonLoad.Click
        If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
            Dim lines = File.ReadAllLines(OpenFileDialog1.FileName)
            ListBox1.Items.Clear()
            ListBox1.Items.AddRange(lines)
        End If
    End Sub
End Class

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM