0

我有 2 个文本文件,命名为 sQue.txt每行中包含单个单词(每行中的每个单词),并且每行中sObj.txt也包含单个单词(但此文件中的条目数比 sQue.txt 中的多)。

现在,我有一个空白表格,我想在其中读取上述两个文件并以如下方式显示它们:

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

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

例子:

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 和 ccc 的标签。每个框中还包含 3 个 CheckedListBoxes 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
4

2 回答 2

1

如果您使用 groupbox,您可以使用 text 属性作为您的标签,并将选中的列表框添加到包含您想要的项目的 groupbox 中。此代码将执行此操作:

Imports System.IO

Public Class Form1

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Dim NewForm2 As New Form2
        NewForm2.Show()
        Dim sObj() As String = File.ReadAllLines("sobj.txt")
        Dim sQue() As String = File.ReadAllLines("sQue.txt")
        For Each s As String In sObj
            Me.Controls.Add(MakeNewGB(s, sQue))
        Next

    End Sub
End Class
Public Module Module1
    Friend WithEvents NewGB As System.Windows.Forms.GroupBox
    Friend WithEvents NewCLB As System.Windows.Forms.CheckedListBox
    Public NextColumn As Integer = 0
    Public Function MakeNewGB(lbl As String, clbItems() As String) As GroupBox
        NewGB = New System.Windows.Forms.GroupBox()
        NewCLB = New System.Windows.Forms.CheckedListBox()
        NewGB.SuspendLayout()
        'GroupBox1
        '
        NewGB.Controls.Add(NewCLB)
        NewGB.Location = New System.Drawing.Point(NextColumn, 0)
        NewGB.Name = lbl
        NewGB.Size = New System.Drawing.Size(126, 210)
        NewGB.TabIndex = 0
        NewGB.TabStop = False
        NewGB.Text = lbl
        '
        'CheckedListBox1
        '
        NewCLB.FormattingEnabled = True
        NewCLB.Location = New System.Drawing.Point(6, 19)
        NewCLB.Name = "clb" + lbl
        NewCLB.Size = New System.Drawing.Size(103, 184)
        NewCLB.TabIndex = 0
        NewCLB.Items.AddRange(clbItems)
        NextColumn += NewGB.Size.Width + 10
        Return NewGB
    End Function
End Module
于 2013-05-27T19:00:01.077 回答
0

我认为你的代码应该是这样的。但我不确定它的目的是什么。

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
       Dim label as new Label
       label.Text = queue(i)
       Dim chklst as new CheckedListBox

       For j=0 to objects.Count - 1
         'create CheckedListBoxes
         chklst.Items.Add(object(j))
       Next
       Me.Controls.Add(label)
       Me.Controls.Add(chklst)
    Next
End Sub
于 2013-05-27T18:00:16.713 回答