无法将控件添加到TableLayoutPanel

时间:2015-10-16 20:34:59

标签: vb.net visual-studio-2010 tablelayoutpanel

以下代码仅适用于第一行。进入下一行后,它会显示在底部并向上移动。我之前在另一个应用程序中完成了这个,所以我引用了该代码。然而,我得到了不同的结果。我希望行从上到下,而不是从下到上。

首先是

变量whichcolumn = 2,因为前两个文本框已经创建。变量whichRow = 0。变量i = 2并且存在以为所有控件提供唯一名称。所有列都在运行时之前存在。

 Private Sub Add_Click(sender As System.Object, e As System.EventArgs) Handles addBTN.Click
        If whichColumn < 8 Then
            Dim startTXT As New TextBox
            Dim endTXT As New TextBox
            startTXT.Name = "start" & i & "TXT"
            endTXT.Name = "end" & i & "TXT"
            rangesPanel.Controls.Add(startTXT, whichColumn, whichRow)
            whichColumn += 1
            rangesPanel.Controls.Add(endTXT, whichColumn, whichRow)
            whichColumn += 1
            If Label3.Visible = False Then
                Label3.Visible = True
                Label4.Visible = True
            ElseIf Label5.Visible = False Then
                Label5.Visible = True
                Label6.Visible = True
            ElseIf Label7.Visible = False Then
                Label7.Visible = True
                Label8.Visible = True
            End If
            i += 1
        Else
            rangesPanel.RowStyles.Add(New RowStyle(SizeType.AutoSize))
            rangesPanel.RowCount += 1
            whichColumn = 0
            whichRow += 1
            Dim startTXT As New TextBox
            Dim endTXT As New TextBox
            startTXT.Name = "start" & i & "TXT"
            endTXT.Name = "end" & i & "TXT"
            rangesPanel.Controls.Add(startTXT, whichColumn, whichRow)
            whichColumn += 1
            rangesPanel.Controls.Add(endTXT, whichColumn, whichRow)
            whichColumn += 1
            i += 1
        End If
    End Sub

enter image description here

1 个答案:

答案 0 :(得分:0)

Private Sub Add_Click(sender As System.Object, e As System.EventArgs) Handles addBTN.Click
    If whichColumn < 8 Then
        Dim startTXT As New TextBox
        Dim endTXT As New TextBox
        startTXT.Name = "start" & i & "TXT"
        endTXT.Name = "end" & i & "TXT"
        rangesPanel.Controls.Add(startTXT, whichColumn, whichRow)
        whichColumn += 1
        rangesPanel.Controls.Add(endTXT, whichColumn, whichRow)
        whichColumn += 1
        If Label3.Visible = False Then
            Label3.Visible = True
            Label4.Visible = True
        ElseIf Label5.Visible = False Then
            Label5.Visible = True
            Label6.Visible = True
        ElseIf Label7.Visible = False Then
            Label7.Visible = True
            Label8.Visible = True
        End If
        i += 1
    Else
        rangesPanel.RowStyles.Add(New RowStyle(SizeType.Absolute, 22))
        rangesPanel.RowCount += 1
        whichColumn = 0
        whichRow += 1
        Dim startTXT As New TextBox
        Dim endTXT As New TextBox
        startTXT.Name = "start" & i & "TXT"
        endTXT.Name = "end" & i & "TXT"
        rangesPanel.Controls.Add(startTXT, whichColumn, whichRow)
        whichColumn += 1
        rangesPanel.Controls.Add(endTXT, whichColumn, whichRow)
        whichColumn += 1
        i += 1
    End If
End Sub

在所有行上将SizeTypePercent更改为Absolute,解决了这个问题。