Creating a TablePanelLayout in run-time

Im trying to create a TablePanelLayout during run-time, and add 2 link buttons to it, but I keep getting errors about the coloum number not been valid

Any ideas what Im doing wrong?
Dim newTableLayout As New TableLayoutPanel
            newTableLayout.RowCount = 4
            newTableLayout.ColumnCount = 2
            newTableLayout.ColumnStyles(0).SizeType = SizeType.Absolute
            newTableLayout.ColumnStyles(0).Width = 20
            newTableLayout.ColumnStyles(1).SizeType = SizeType.AutoSize

            Dim newLabel1 As New LinkLabel
            Dim newLabel2 As New LinkLabel

            newLabel1.Text = " "
            newLabel1.Text = "+"

            AddHandler newLabel1.Click, AddressOf clickButton
            AddHandler newLabel2.Click, AddressOf clickButton

            newTableLayout.Controls.Add(newLabel1, 0, 0)
            newTableLayout.Controls.Add(newLabel1, 0, 1)

Open in new window

tonelm54Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

AshokSr. Software EngineerCommented:
Dim newTableLayout As New TableLayoutPanel
            newTableLayout.RowCount = 4
            newTableLayout.ColumnCount = 2
            newTableLayout.ColumnStyles(0).SizeType = SizeType.Absolute
            newTableLayout.ColumnStyles(0).Width = 20
            newTableLayout.ColumnStyles(1).SizeType = SizeType.AutoSize

            Dim newLabel1 As New LinkLabel
            Dim newLabel2 As New LinkLabel

            newLabel1.Text = " "
            newLabel1.Text = "+"

            AddHandler newLabel1.Click, AddressOf clickButton
            AddHandler newLabel2.Click, AddressOf clickButton

            newTableLayout.Controls.Add(newLabel1, 0, 0)
            newTableLayout.Controls.Add(newLabel2, 0, 1)

HTH
Ashok
AshokSr. Software EngineerCommented:
On which line do you get an error?

Dim newTableLayout As New TableLayoutPanel
            newTableLayout.RowCount = 4
            newTableLayout.ColumnCount = 2
            newTableLayout.ColumnStyles(0).SizeType = SizeType.Absolute       ' not sure about
            newTableLayout.ColumnStyles(0).Width = 20                                   ' this three
            newTableLayout.ColumnStyles(1).SizeType = SizeType.AutoSize      ' lines here.

            Dim newLabel1 As New LinkLabel
            Dim newLabel2 As New LinkLabel

            newLabel1.Text = " "
            newLabel2.Text = "+"

            AddHandler newLabel1.Click, AddressOf clickButton
            AddHandler newLabel2.Click, AddressOf clickButton

            newTableLayout.Controls.Add(newLabel1, 0, 0)
            newTableLayout.Controls.Add(newLabel2, 0, 1)
Me.Controls.Add(newTableLayout)
AshokSr. Software EngineerCommented:
Also I think you need to specify

      newTableLayout.Location = new Point(10, 40)
      newTableLayout.Size = new Size(200, 200)

Ashok
Introduction to Web Design

Develop a strong foundation and understanding of web design by learning HTML, CSS, and additional tools to help you develop your own website.

Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
You have to actually create the ColumnStyles first:
        Dim newTableLayout As New TableLayoutPanel
        newTableLayout.ColumnStyles.Add(New ColumnStyle(SizeType.Absolute, 20))
        newTableLayout.ColumnStyles.Add(New ColumnStyle(SizeType.AutoSize))

        Dim newLabel1 As New LinkLabel
        Dim newLabel2 As New LinkLabel

        newLabel1.Text = " "
        newLabel1.Text = "+"

        AddHandler newLabel1.Click, AddressOf clickButton
        AddHandler newLabel2.Click, AddressOf clickButton

        newTableLayout.Controls.Add(newLabel1, 0, 0)
        newTableLayout.Controls.Add(newLabel1, 0, 1)

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
AshokSr. Software EngineerCommented:
I would suggest you place TableLayoutpanel named "newTableLayout" at Design time in the form.  It would save lot of headache.

        Dim newLabel1 As New LinkLabel
        Dim newLabel2 As New LinkLabel
        newLabel1.Size = new Size(125, 18)
        newLabel2.Size = new Size(125, 18)
        newLabel1.Text = " "
        newLabel2.Text = "+"

        AddHandler newLabel1.Click, AddressOf clickButton
        AddHandler newLabel2.Click, AddressOf clickButton

            newTableLayout.Controls.Add(newLabel1, 0, 0)  ' col, row
            newTableLayout.Controls.Add(newLabel2, 1, 2)
            Me.Controls.Add(newTableLayout)

HTH
Ashok
AshokSr. Software EngineerCommented:
Sorry, you do not need to add using "Me.Controls.Add(newTableLayout)".
I would suggest you place TableLayoutpanel named "newTableLayout" at Design time in the form.  It would save lot of headache.

        Dim newLabel1 As New LinkLabel
        Dim newLabel2 As New LinkLabel
        newLabel1.Size = new Size(125, 18)
        newLabel2.Size = new Size(125, 18)
        newLabel1.Text = " "
        newLabel2.Text = "+"

        AddHandler newLabel1.Click, AddressOf clickButton
        AddHandler newLabel2.Click, AddressOf clickButton

           newTableLayout.Controls.Add(newLabel1, 0, 0)  ' col, row
           newTableLayout.Controls.Add(newLabel2, 1, 2)

HTH
Ashok
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.