Vb.net从文本文件加载多行到不同的列表框

时间:2016-05-04 06:36:01

标签: vb.net

简介

好吧,我有一个load和一个save按钮。我要将所有listbox items保存到.txt文件,然后将其加载到列表框中。

现在,如果只是一个列表框,这将非常简单。我的问题是它的9个列表框,只有一个包含9个列表框中所有项目的.txt文件。我不想要9个不同的.txt文件。请记住,列表框中包含的项目有多少。

这是save按钮输出到.txt文件(另一个版本向下)的方式

Channel 1
c:\folder\folder

Channel 2
\\server\folder\foldera
\\server\folder\folderb

Channel 3
\\anotherserver\foldera
\\anotherserver\folderb
\\anotherserver\folderc
\\anotherserver\folderd
\\anotherserver\foldere

Channel 4
\\somepath\folder\folder\foldera

Channel 5
\\server\folder\foldera

Channel 6
\\server\folder\foldera

\\server\folder\foldera

Channel 8
\\server\folder\foldera

Channel 9
\\server\folder\foldera

每个频道都是列表框,路径是每个列表框包含的项目。

这是我的保存btnsave.click

Dim configfile As String = "C:\Applicationpath\Config\" & System.DateTime.Now.ToString("yyyyMMdd") & "_Config.txt"

Using fs As FileStream = File.Create(configfile)
fs.Close()
End Using

Write_Configfile()

...而Write_config()执行以下操作:

    Dim objWriter As New System.IO.StreamWriter(configfile, True)
    Try
        objWriter.WriteLine("Channel 1")
        For Each itm1 As String In Me.lbchannel1.Items
            objWriter.WriteLine(itm1)
        Next
        objWriter.WriteLine(" ")

        objWriter.WriteLine("Channel 2")
        For Each itm2 As String In Me.lbChannel2.Items
            objWriter.WriteLine(itm2)
        Next
        objWriter.WriteLine(" ")

并且一直到第9频道。我知道,这可能是一种更简单的方法。

问题和疑问

现在转到load按钮。我想如果我可以从文本文件的特定部分加载每个列表框,我会没事的。我想说“嘿,将所有项目添加到配置文件中”通道1“和”通道2“之间的通道1(列表框1) - 依此类推到通道9.这就是我所在的地方卡住了。

在某个词之后或某些词之间我需要readline。没有阅读某一行,因为就像我说的那样,列表框中包含的项目有多少。

请记住,如果更容易,我可以将保存输出更改为:

c1:c:\folder\folder   
c2:\\server\folder\foldera
c2:\\server\folder\folderb
'and so on... 

所以我的尝试就是这样:

    If OpenFileDialog1.ShowDialog = DialogResult.OK Then

        Using reader As New StreamReader(OpenFileDialog1.FileName.ToString)
            Dim line As String = reader.ReadLine(OpenFileDialog1.FileName)
            If line = "c1:" Then
                lbchannel1.Items.Add(line) 'lbchannel1 is listbox1
            End If
            'if line = "c2:" then
            'blablabla
            'if line = "c3:" then
            'same goes to c9
        End Using
    End If

但是这给了我这个错误{"Conversion from string C:\Applicationpath\Config\20160405_config.txt to type integer is not valid."},我确信它不是正确的方法。

3 个答案:

答案 0 :(得分:2)

对于您的保存功能,您可以使用以下内容:

Using objWriter As New System.IO.StreamWriter(configfile, True)
    Try
        'We will run through each channel
        For i = 1 to 9
            objWriter.WriteLine("Channel " & i.ToString())
            'We find the ListBox based on its name
            Dim LB As ListBox = CType(Me.Controls.Find("lbchannel" & i.ToString(), True), ListBox)
            For Each itm As String In LB.Items
                objWriter.WriteLine(itm)
            Next
            objWriter.WriteLine("")
        Next
    Catch ex as Exception
        'Exception Handling here
    End Try
End Using

然后您将要按以下方式加载文件:

Using reader As New StreamReader(OpenFileDialog1.FileName)
    Dim Line As String = reader.ReadLine()
    Dim CurrentChanel as Integer = 0
    Dim LB As ListBox = Nothing
    While Not IsNothing(Line) 'It will be Nothing when file is over
        If Line.StartsWith("Channel ") Then
            'We will increment CurrentChannel, as we changed the section
            CurrentChannel += 1
            LB = CType(Me.Controls.Find("lbchannel" & CurrentChannel.ToString(), True), ListBox)
        Else If Line <> "" Then
            LB.Items.Add(Line)
        End If
        Line = reader.ReadLine()
    End While
End Using

答案 1 :(得分:2)

如果您调整列表框的名称和文本文件中的标题以使它们相同,那么就有一个通用的解决方案。

更改Write_config(),使其成为Channel1而不是Channel 1等。

Dim objWriter As New System.IO.StreamWriter(configfile, True)
For Each o As ListBox In Me.Controls.OfType(Of ListBox).Where(Function(c) c.Name.StartsWith("Channel"))
    objWriter.WriteLine(o.Name)
    For Each itm As String In o.Items
        objWriter.WriteLine(itm1)
    Next
Next

同时将列表框控件的名称更改为Channel1,最多为Channel9

新TextFile看起来像:

Channel1
c:\folder\folder

Channel2
\\server\folder\foldera
\\server\folder\folderb
...

现在更改填充列表框的代码:

Using reader As New StreamReader(OpenFileDialog1.FileName.ToString)
        Dim currentListBox As ListBox = Nothing

        While reader.Peek > -1
            Dim line As String = reader.ReadLine()
            If Not String.IsNullOrEmpty(line) Then
                Dim tmpListBox = Controls.Find(line, false) 'Try to find listbox according to line 
                If tmpListBox.Any() Then 'It´s a listbox name
                   currentListBox = DirectCast(tmpListBox(0), ListBox)
                Else 'It´s a folder name
                   currentListBox.Items.Add(line) 
                End If
            End If
       End While
 End Using

答案 2 :(得分:1)

从ReadLine中删除文件名。 然后添加Split和Select Case以确定要填充的控件。

        Using reader As New StreamReader(OpenFileDialog1.FileName.ToString)
            Dim line As String = reader.ReadLine()
            Dim values() As String = line.Split(":")
            Select Case values(0)
                Case "C1"
                    lbchannel1.items.add(values(1))

                Case "C2"
                    lbchannel2.items.add(values(1))
            End Select

        End Using