1

我正在尝试创建标签和文本框列表。没有错误,但它们没有呈现在表单上。我已经确认我的循环有值

Private Sub AddLabels_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    'MsgBox(strNumberOfLabels + " " + strOrderNum)
    TableLayoutPanel1.AutoSize = True
    TableLayoutPanel1.Visible = False
    TableLayoutPanel1.SuspendLayout()
    For i As Integer = 0 To strNumberOfLabels

        'MsgBox(i)
        Dim txtBox As New TextBox
        Dim txtLabel As New Label
        txtLabel.Text = "Label " + i
        txtBox.Name = "txt" + i
        TableLayoutPanel1.Controls.Add(txtLabel)
        txtLabel.Show()
        txtBox.Show()
        TableLayoutPanel1.ResumeLayout()
        TableLayoutPanel1.Visible = True
    Next
End Sub
4

1 回答 1

0

尝试使用另一个Add重载,它指定控件应该进入哪一列和哪一行:

Dim txtLabel As New Label
txtLabel.Text = "Label" + i.ToString
'\\ TableLayoutPanel1.Controls.Add(txtLabel)
TableLayoutPanel1.Controls.Add(txtLabel, 0, 0)

这不是必需的:

'\\ txtLabel.Show()

这应该移到循环之外:

  Next
TableLayoutPanel1.ResumeLayout()
'\\ TableLayoutPanel1.Visible = True

控件永远不会被txtBox添加到TableLayoutPanel控件或窗体中。

我认为在此OnLoad过程中没有必要让您的 TableLayoutPanel 可见或不可见,表单尚不可见。

唯一需要确认的是 的值strNumberOfLabels。我怀疑它是零。

于 2012-04-18T01:07:05.593 回答