动态控件无法在WinForm中呈现

时间:2012-04-18 00:56:25

标签: vb.net dynamic controls

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

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

1 个答案:

答案 0 :(得分: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的价值。我怀疑它是零。