动态阅读文件&显示表单加载

时间:2013-05-27 15:38:40

标签: vb.net

我有2个名为sQue.txt的文本文件,每行包含单个单词(每行中的每个单词),sObj.txt每行也包含单个单词(但条目中没有更多条目)文件比在sQue.txt中。。

现在,我有一个空白表格,我想要阅读上述文件和以下列方式显示它们:

  1. sQue.txt文件中的每个条目都以单独的标签显示在

  2. 文件sObj.txt的所有条目都放在CheckedListBox&对于上面第1点中显示的每个标签,都会显示此CheckedListBox。

  3. 示例:

    sObj.txt contains 3 entries aaa, bbb & ccc (vertically i.e each in new line). sQue.txt contains 5 entries p,q,r,s & t (vertically i.e each in new line).

    现在,当表单加载时,会看到3个带有文本aaa,bbb& amp;标签的标签。 CCC。此外,还有3个CheckedListBox在每个框中都包含p,q,r,s & t

    可以吗?我正试图找到一个没有运气的解决方案。

    请帮忙。

    直到现在我所拥有的只是

    Private Sub Form7_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim queue As String() = IO.File.ReadAllLines("C:\temp\sQue.txt")
        Dim objects As String() = IO.File.ReadAllLines("C:\temp\sObj.txt")
    
        For i = 0 To queue.Count - 1
        'create labels here
           For j=0 to objects.Count - 1
           'create CheckedListBoxes
           Next
        Next
    End Sub
    

1 个答案:

答案 0 :(得分:0)

很容易做到:

Private Sub Form7_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Dim theAppDirectory = IO.Path.GetDirectoryName(Application.ExecutablePath)

    Dim queue As String() = IO.File.ReadAllLines(theAppDirectory & "\que.txt")
    Dim objects As String() = IO.File.ReadAllLines(theAppDirectory & "\obj.txt")

    Dim top As Integer = 10
    Dim left As Integer = 10
    Dim I As Integer = 0
    Dim J As Integer = 0

    Dim aLabel As Label
    Dim aListCheckBox As CheckedListBox

    Dim aPanel As New Panel
    aPanel.Dock = DockStyle.Fill
    aPanel.Top = 0
    aPanel.Left = 0
    aPanel.AutoScroll = True

    For I = 0 To queue.Count - 1
        aLabel = New Label
        aLabel.Text = queue(I)
        aLabel.Top = top
        aLabel.Left = left

        aListCheckBox = New CheckedListBox
        aListCheckBox.Top = top
        aListCheckBox.Left = left + 100
        For J = 0 To objects.Count - 1
            aListCheckBox.Items.Add(objects(J), False)
            top += 20
        Next J

        'add event handlers here

        aPanel.Controls.Add(aLabel)
        aPanel.Controls.Add(aListCheckBox)
    Next I

    Me.Controls.Add(aPanel)
End Sub

这假定您希望文件与可执行文件位于同一目录中。

也没有添加事件处理程序。您需要确定所需的事件处理程序,并在创建控件时添加它们。