-1

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

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

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

4

1 回答 1

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
于 2013-08-04T00:09:02.427 回答