折叠面板后添加新文本框

时间:2013-08-03 21:40:45

标签: .net vb.net splitcontainer

我有一个在我的表单上创建用户的按钮。单击该按钮时,会执行此操作:

Me.SplitContainer1.Panel1Collapsed = True
Me.BtnSave.Tag = "addNew"
Me.txtUserName.Text = ""
Me.txtPassword.Text = ""
Me.txtRole.Text = ""

然后我得到3个文本框和两个按钮(保存并退出)。如果我想添加一个新的文本框,我该怎么办?

1 个答案:

答案 0 :(得分:0)

我不确定我是否完全了解您的情况,但您可以在运行时添加控件,如下所示:

Dim txtNewTextBox As TextBox = New TextBox() 'create and initialize
txtNewTextBox.Parent = SplitContainer1.Panel1 'set its parent
txtNewTextBox.Location = New Point(12, 50) 'location based on the parent location

存储对它的引用可能是明智的,因此您可以在创建它的范围之外访问它。

Public Class Form1
    Dim txtNewTextBoxRef As TextBox = Nothing

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim txtNewTextBox As TextBox = New TextBox()
        txtNewTextBox.Parent = SplitContainer1.Panel1 
        txtNewTextBox.Location = New Point(12, 50) 
        txtNewTextBoxRef = txtNewTextBox
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        MsgBox("txtNewTextBox.text = " & txtNewTextBoxRef.Text)
    End Sub
End Class