none
請問如何將txt資料加入listbox RRS feed

  • 問題

  • 目前程式能將listbox1的資料寫入txt

    例如lisxbox1 存為*.txt 為下面資料

    111,265,000

    112.260.111

    113.255.111

    Private Sub Button11_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button11.Click
            Dim saveFileDialog1 As New SaveFileDialog()
            Dim filename As String
            saveFileDialog1.Filter = "ini files (*.ini)|*.ini|txt files (*.txt)|*.txt"
            saveFileDialog1.FilterIndex = 1
            saveFileDialog1.RestoreDirectory = True


            If saveFileDialog1.ShowDialog() = DialogResult.OK Then
                filename = saveFileDialog1.FileName
                Dim i, J As Integer
                Dim sw As New System.IO.StreamWriter(filename, False, System.Text.Encoding.Default)
                Dim dstring As String
                J = ListBox1.Items.Count

                For i = 0 To J - 1
                    dstring = ListBox1.Items(i)
                    sw.WriteLine(dstring)
                    sw.Flush()

                Next
                sw.Close()
            End If
        End Sub

    那如何將*.txt裡面的

    111,265,000

    112.260.111

    113.255.111

    讀回到listbox2下面需要改那裡呢

       Private Sub Button10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button10.Click

            Dim OpenFileDialog1 As New OpenFileDialog
            OpenFileDialog1.Filter = "ini files (*.ini)|*.ini|txt files (*.txt)|*.txt"
            OpenFileDialog1.FilterIndex = 1
            OpenFileDialog1.RestoreDirectory = True
            Dim filename As String

            If OpenFileDialog1.ShowDialog = DialogResult.OK Then
                filename = OpenFileDialog1.FileName
                Dim sw As New System.IO.StreamReader(filename, System.Text.Encoding.Default)
               
                If sw IsNot Nothing Then
                    For Each item In sw
                        ListBox2.Items.Add(item)
                    Next
                End If
               
            End If
        End Sub

    2015年1月18日 上午 09:12

解答

  • 不好意思 我goolge上網找到解答了^^"

    將txt加入listbox裡

     Private Sub Button10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button10.Click
            Dim OpenFileDialog1 As New OpenFileDialog
            OpenFileDialog1.Filter = "ini files (*.ini)|*.ini|txt files (*.txt)|*.txt"
            OpenFileDialog1.FilterIndex = 1
            OpenFileDialog1.RestoreDirectory = True
            Dim filename As String

            If OpenFileDialog1.ShowDialog = DialogResult.OK Then
                filename = OpenFileDialog1.FileName

                Dim dstring As IO.StreamReader = New IO.StreamReader(filename, System.Text.Encoding.Default)
                ListBox2.Items.Clear()
                Do Until dstring.EndOfStream
                    ListBox2.Items.Add(dstring.ReadLine())
                Loop
                dstring.Close()
              
            End If
        End Sub

    2015年1月18日 上午 10:15