Group, you were very helpful to me in adding multiple textboxes, labels, etc., in my original post. If you would like to read through that, see http://www.daniweb.com/software-development/vbnet/threads/447985/creating-multiple-labels-textboxes-etc.-dynamically#post1934799.

In a similar vein, I now want to dynamically add these multiple panels within a "main" panel, one at a time, with the use of a button control.

I have created the first panel within the main panel on the form itself. Using the code the group and I developed in the original post (see the link above), I have the program generating the additional, identical forms. That code looks like this:

Private Sub btnCreateNewLine_(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCreateNewLine.Click

        Dim b As String = Short.Parse(a)

        'This creates the label "Part No."

        Dim lblPrtNo As New Label()
        lblPrtNo.Name = "lblPrtNo" & Convert.ToString(b)
        lblPrtNo.Text = "Part Number"
        lblPrtNo.ForeColor = Color.White
        lblPrtNo.Font = New Font("Sans Serif", 9)
        lblPrtNo.Font = New Font(lblPrtNo.Font, FontStyle.Regular)
        lblPrtNo.Location = New Point(13, 8)
        lblPrtNo.Size = New Size(77, 15)
        Me.Controls.Add(lblPrtNo)
        '
        'This generates the textbox for the user to enter the Part No.
        Dim txbPartNo As New TextBox()
        txbPartNo.Name = "txbPartNo" & Convert.ToString(b)
        txbPartNo.Text = ""
        txbPartNo.ForeColor = Color.Black
        txbPartNo.BackColor = Color.Yellow
        txbPartNo.Font = New Font("Sans Serif", 10)
        txbPartNo.Font = New Font(txbPartNo.Font, FontStyle.Bold)
        txbPartNo.Location = New Point(16, 26)
        txbPartNo.Size = New Size(263, 22)
        txbPartNo.Cursor = Cursors.Hand
        txbPartNo.AcceptsReturn = True
        txbPartNo.AcceptsTab = True
        txbPartNo.TabIndex = 10
        AddHandler txbPartNo.TextChanged, AddressOf txbPartNo_Textchanged
        Me.Controls.Add(txbPartNo)
        '
        ' This generates the button control to do a part number search
        Dim btnPartNoSearch As New Button()
        btnPartNoSearch.Name = "btnPartNoSearch"
        btnPartNoSearch.Text = "S"
        btnPartNoSearch.FlatStyle = FlatStyle.Popup
        btnPartNoSearch.FlatAppearance.BorderColor = Color.Black
        btnPartNoSearch.FlatAppearance.BorderSize = 2
        btnPartNoSearch.Cursor = Cursors.Hand
        btnPartNoSearch.TabIndex = 11
        btnPartNoSearch.ForeColor = Color.White
        btnPartNoSearch.BackColor = Color.Red
        btnPartNoSearch.Size = New Size(26, 23)
        btnPartNoSearch.Location = New Point(285, 26)
        btnPartNoSearch.Visible = True
        Me.Controls.Add(btnPartNoSearch)

        'This creates the Panel and sets the labels and Textboxes to be displayed.
        Dim pnlOrderLine As New Panel()
        pnlOrderLine.Size = New Size(1577, 122)
        pnlOrderLine.Location = New Point(12, x)
        pnlOrderLine.BorderStyle = BorderStyle.Fixed3D
        pnlOrderLine.ForeColor = Color.White
        pnlOrderLine.Controls.Add(lblPrtNo)
        pnlOrderLine.Controls.Add(txbPartNo)
        pnlOrderLine.Controls.Add(btnPartNoSearch)
        pnlOrderLine.Visible = True
        Me.Controls.Add(pnlOrderLine)

        Private Sub txbPartNo_Textchanged(ByVal sender As System.Object, ByVal e As EventArgs)
        Dim txbPartNo As TextBox = DirectCast(sender, TextBox)
        Me.Text = txbPartNo.Name & ": " & txbPartNo.Text
        txbPartNo.Show()

Now the challenge is to get the second panel (remember, the first was created on the form) to display within this main panel that I am calling "pnlMainPanel". To do this, my assumption is that I use similar logic as we did to "copy" the original panel. So I wrote this"

 ' This set the order line panel within the main panel
        Dim pnlMainPanel As New Panel()
        pnlMainPanel.Controls.Add(pnlOrderLine)
        pnlMainPanel.Visible = True
        Me.Controls.Add(pnlOrderLine)

Unfortunately, the subsequent panels are created, but they are outside and behind this "main" panel. How do I get these inside the main panel?

In advance, thanks for all the assistance.

Don

Recommended Answers

All 4 Replies

Hi you have added the panel twice once to the Main panel and once to the form:

 'Adding to main panel
 pnlMainPanel.Controls.Add(pnlOrderLine)
 pnlMainPanel.Visible = True
 'Adding to Form i.e. me
 Me.Controls.Add(pnlOrderLine)

Mr. Waddell,

I inserted the code you wrote and tested it. It continues to create the "pnlOrderLine" outside and behind the main panel (pnlMainPanel).

It's back to the drawing board.

Thanks again...

Don

Great example. Now a have a question and another problem.

I do not see the End Sub for the btnCreateNewLine_Click Sub.

Where do I write the txbPartNo_Textchanged Sub

The controls in the child panel. How can a read the text in the texbox in a MsgBox or in another TextBox ?.

Also Variable a and x not declare.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.