TablePanelLayout problem - Probably a logic problem

This is probably a logic problem, however Im not sure where I have gone wrong in the logic.

Currently this is just to see if a larger project is possible, the idea is to have a table and when a user clicks on a + a new row is inserted under the +, and a new tablelayout is created in the new row in the second coloum, and everything else moves down.

The form loads great, and the first time the tablepanel works great, removing and inserting correctly, however when the user selects a + in the newly created tablepanellayout it goes wrong, and the third nested tablepanellayout is created right aligned I think, and wont auto-size anymore.

Is it possible someone can have a look at my logic, and point out where Im going wrong please?

Thanks in advance!!
TablePanelLayout-add-remove-row-.zip
tonelm54Asked:
Who is Participating?
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.

CodeCruiserCommented:
Can you upload a fully compileable project?
0
tonelm54Author Commented:
Good morning,
Thanks for looking CodeCruiser.
TablePanelLayout-add-remove-row-.zip
0
tonelm54Author Commented:
Ive managed to fix the right align issue, bt adding the line:-
 newTableLayout.CellBorderStyle = TableLayoutPanelCellBorderStyle.Single and looking how the layout is planned, but Im having issues still with the colapsing of the tables.

It should delete the controls in the row below the clicked "-" and everything below that move up and then delete the bottom row of that table.

My updated code is attached, if you can have 5 minutes can you look at it, probably just having another pair of eyes looking can fix my logic error.

Thank you
Imports System.Windows.Forms

Public Class Form1
    Dim cellCnt As Integer
    Dim tr1 As New TextWriterTraceListener(System.IO.File.CreateText("trace.txt"))

    Private Sub Form1_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        Trace.Flush()

    End Sub

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        AddHandler LinkLabel1.Click, AddressOf clickButton
        AddHandler LinkLabel2.Click, AddressOf clickButton
        AddHandler LinkLabel3.Click, AddressOf clickButton
        AddHandler LinkLabel4.Click, AddressOf clickButton

        For x = 0 To 4 Step 1
            Dim tmpLabel As New Label
            cellCnt = cellCnt + 1
            tmpLabel.Text = cellCnt
            TableLayoutPanel1.Controls.Add(tmpLabel, 1, x)
        Next


        Debug.Listeners.Add(tr1)


    End Sub

    Private Sub clickButton(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim link As LinkLabel = CType(sender, LinkLabel)
        If link.Text = "+" Then
            link.Text = "-"
            Dim innerTLP As TableLayoutPanel = link.Parent
            Debug.WriteLine("link.parent=" & link.Parent.ToString)

            Dim pos As TableLayoutPanelCellPosition = innerTLP.GetCellPosition(link)

            For Each ExistControl In innerTLP.Controls
                If innerTLP.GetRow(ExistControl) > pos.Row Then
                    innerTLP.SetRow(ExistControl, innerTLP.GetRow(ExistControl) + 1)
                End If
            Next

            Dim newTableLayout As New TableLayoutPanel
            newTableLayout.RowCount = 4
            newTableLayout.RowStyles.Add(New RowStyle(SizeType.AutoSize))

            newTableLayout.CellBorderStyle = TableLayoutPanelCellBorderStyle.Single

            newTableLayout.ColumnStyles.Add(New ColumnStyle(SizeType.Absolute, 20))
            newTableLayout.ColumnStyles.Add(New ColumnStyle(SizeType.AutoSize))

            For x = 1 To 4 Step 1
                Dim newLabel1 As New LinkLabel
                If x Mod 2 = 0 Then
                    newLabel1.Text = "-"
                Else
                    newLabel1.Text = "+"
                End If

                cellCnt = cellCnt + 1
                Dim tmpLabel As New Label
                tmpLabel.Text = cellCnt
                newTableLayout.Controls.Add(tmpLabel, 1, x)
                AddHandler newLabel1.Click, AddressOf clickButton
                newTableLayout.Controls.Add(newLabel1, 0, x)
            Next

            Dim parentLayoutPanel As TableLayoutPanel
            parentLayoutPanel = link.Parent

            parentLayoutPanel.Controls.Add(newTableLayout, 1, pos.Row + 1)
        ElseIf link.Text = "-" Then
            Dim innerTLP As TableLayoutPanel = link.Parent
            Dim pos As TableLayoutPanelCellPosition = innerTLP.GetCellPosition(link)

            For Each ExistControl In TableLayoutPanel1.Controls
                If TableLayoutPanel1.GetRow(ExistControl) = pos.Row + 1 Then
                    TableLayoutPanel1.Controls.Remove(ExistControl)
                End If
            Next

            For Each ExistControl In TableLayoutPanel1.Controls
                If TableLayoutPanel1.GetRow(ExistControl) > pos.Row + 1 Then
                    TableLayoutPanel1.SetRow(ExistControl, TableLayoutPanel1.GetRow(ExistControl) - 1)
                End If
            Next
            innerTLP.RowCount = innerTLP.RowCount - 1

            link.Text = "+"
        End If
    End Sub
End Class

Open in new window

0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

tonelm54Author Commented:
So this is my theory for the collapsing:-

'Find the table where the "-" has been clicked
            Dim innerTLP As TableLayoutPanel = link.Parent
'Get the cell where the "-" button has been clicked
            Dim pos As TableLayoutPanelCellPosition = innerTLP.GetCellPosition(link)
'Work through the table and any control in the row below the '-' button get rid of.
            For Each ExistControl In TableLayoutPanel1.Controls
                If TableLayoutPanel1.GetRow(ExistControl) = pos.Row + 1 Then
                    TableLayoutPanel1.Controls.Remove(ExistControl)
                End If
            Next
'Move any control below the deleted row up one row
            For Each ExistControl In TableLayoutPanel1.Controls
                If TableLayoutPanel1.GetRow(ExistControl) > pos.Row + 1 Then
                    TableLayoutPanel1.SetRow(ExistControl, TableLayoutPanel1.GetRow(ExistControl) - 1)
                End If
            Next
'By now the last row should be empty and can now be binned off.
            innerTLP.RowCount = innerTLP.RowCount - 1
'Change the button text to "+" ready for expanding again if needed.
0
CodeCruiserCommented:
I tried your code but due to the work pressure I have currently, I could not focus well on the logic. I will try again.
0
tonelm54Author Commented:
No worries, thank you for helping
0
tonelm54Author Commented:
Figured the issue out, it wasn't logical, it was me being dumb!!!!!!,

Only problem now is it works great for about the first 4 drops then begins to take ages to draw/resize all the tables, is there a method to disable the redrawing of the form at the top of my code, and then redraw everything in 1 go?
Imports System.Windows.Forms

Public Class Form1
    Dim cellCnt As Integer
    Dim tr1 As New TextWriterTraceListener(System.IO.File.CreateText("trace.txt"))

    Private Sub Form1_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        Trace.Flush()

    End Sub

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        AddHandler LinkLabel1.Click, AddressOf clickButton
        AddHandler LinkLabel2.Click, AddressOf clickButton
        AddHandler LinkLabel3.Click, AddressOf clickButton
        AddHandler LinkLabel4.Click, AddressOf clickButton

        For x = 0 To 4 Step 1
            Dim tmpLabel As New Label
            cellCnt = cellCnt + 1
            tmpLabel.Text = cellCnt
            TableLayoutPanel1.Controls.Add(tmpLabel, 1, x)
        Next


        Debug.Listeners.Add(tr1)


    End Sub

    Private Sub clickButton(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim link As LinkLabel = CType(sender, LinkLabel)

        If link.Text = "+" Then
            link.Text = "-"
            Dim innerTLP As TableLayoutPanel = link.Parent
            Debug.WriteLine("link.parent=" & link.Parent.ToString)

            Dim pos As TableLayoutPanelCellPosition = innerTLP.GetCellPosition(link)

            For Each ExistControl In innerTLP.Controls
                If innerTLP.GetRow(ExistControl) > pos.Row Then
                    innerTLP.SetRow(ExistControl, innerTLP.GetRow(ExistControl) + 1)
                End If
            Next

            Dim newTableLayout As New TableLayoutPanel
            newTableLayout.RowCount = 4
            newTableLayout.RowStyles.Add(New RowStyle(SizeType.AutoSize))

            newTableLayout.CellBorderStyle = TableLayoutPanelCellBorderStyle.Single

            newTableLayout.AutoSize = True
            newTableLayout.AutoSizeMode = AutoSizeMode.GrowAndShrink


            newTableLayout.ColumnStyles.Add(New ColumnStyle(SizeType.Absolute, 20))
            newTableLayout.ColumnStyles.Add(New ColumnStyle(SizeType.AutoSize))

            For x = 1 To 4 Step 1
                Dim newLabel1 As New LinkLabel
                If x Mod 2 = 0 Then
                    newLabel1.Text = "-"
                Else
                    newLabel1.Text = "+"
                End If

                cellCnt = cellCnt + 1
                Dim tmpLabel As New Label
                tmpLabel.Text = cellCnt
                newTableLayout.Controls.Add(tmpLabel, 1, x)
                AddHandler newLabel1.Click, AddressOf clickButton
                newTableLayout.Controls.Add(newLabel1, 0, x)
            Next

            Dim parentLayoutPanel As TableLayoutPanel
            parentLayoutPanel = link.Parent

            parentLayoutPanel.Controls.Add(newTableLayout, 1, pos.Row + 1)
        ElseIf link.Text = "-" Then
            Dim innerTLP As TableLayoutPanel = link.Parent
            Dim pos As TableLayoutPanelCellPosition = innerTLP.GetCellPosition(link)

            For Each ExistControl In innerTLP.Controls
                If innerTLP.GetRow(ExistControl) = pos.Row + 1 Then
                    innerTLP.Controls.Remove(ExistControl)
                End If
            Next

            For Each ExistControl In innerTLP.Controls
                If innerTLP.GetRow(ExistControl) > pos.Row + 1 Then
                    innerTLP.SetRow(ExistControl, innerTLP.GetRow(ExistControl) - 1)
                End If
            Next
            innerTLP.RowCount = innerTLP.RowCount - 1

            link.Text = "+"
        End If
    End Sub
End Class

Open in new window

0
CodeCruiserCommented:
Try adding a Me.SuspendLayout() call at start of code and then adding the Me.ResumeLayout() at the end.
0
tonelm54Author Commented:
Nope, still very slow at drawing
Imports System.Windows.Forms

Public Class Form1
    Dim cellCnt As Integer

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        AddHandler LinkLabel1.Click, AddressOf clickButton
        AddHandler LinkLabel2.Click, AddressOf clickButton
        AddHandler LinkLabel3.Click, AddressOf clickButton
        AddHandler LinkLabel4.Click, AddressOf clickButton

        For x = 0 To 4 Step 1
            Dim tmpLabel As New Label
            cellCnt = cellCnt + 1
            tmpLabel.Text = cellCnt
            TableLayoutPanel1.Controls.Add(tmpLabel, 1, x)
        Next



    End Sub

    Private Sub clickButton(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim link As LinkLabel = CType(sender, LinkLabel)

        Me.SuspendLayout()

        If link.Text = "+" Then
            link.Text = "-"
            Dim innerTLP As TableLayoutPanel = link.Parent

            Dim pos As TableLayoutPanelCellPosition = innerTLP.GetCellPosition(link)

            For Each ExistControl In innerTLP.Controls
                If innerTLP.GetRow(ExistControl) > pos.Row Then
                    innerTLP.SetRow(ExistControl, innerTLP.GetRow(ExistControl) + 1)
                End If
            Next

            Dim newTableLayout As New TableLayoutPanel
            newTableLayout.RowCount = 4
            newTableLayout.RowStyles.Add(New RowStyle(SizeType.AutoSize))

            newTableLayout.AutoSize = True
            newTableLayout.AutoSizeMode = AutoSizeMode.GrowAndShrink


            newTableLayout.ColumnStyles.Add(New ColumnStyle(SizeType.Absolute, 20))
            newTableLayout.ColumnStyles.Add(New ColumnStyle(SizeType.AutoSize))

            For x = 1 To 4 Step 1
                Dim newLabel1 As New LinkLabel
                If x Mod 2 = 0 Then
                    newLabel1.Text = "-"
                Else
                    newLabel1.Text = "+"
                End If

                cellCnt = cellCnt + 1
                Dim tmpLabel As New Label
                tmpLabel.Text = cellCnt
                newTableLayout.Controls.Add(tmpLabel, 1, x)
                AddHandler newLabel1.Click, AddressOf clickButton
                newTableLayout.Controls.Add(newLabel1, 0, x)
            Next

            Dim parentLayoutPanel As TableLayoutPanel
            parentLayoutPanel = link.Parent

            parentLayoutPanel.Controls.Add(newTableLayout, 1, pos.Row + 1)
        ElseIf link.Text = "-" Then
            Dim innerTLP As TableLayoutPanel = link.Parent
            Dim pos As TableLayoutPanelCellPosition = innerTLP.GetCellPosition(link)

            For Each ExistControl In innerTLP.Controls
                If innerTLP.GetRow(ExistControl) = pos.Row + 1 Then
                    innerTLP.Controls.Remove(ExistControl)
                End If
            Next

            For Each ExistControl In innerTLP.Controls
                If innerTLP.GetRow(ExistControl) > pos.Row + 1 Then
                    innerTLP.SetRow(ExistControl, innerTLP.GetRow(ExistControl) - 1)
                End If
            Next
            innerTLP.RowCount = innerTLP.RowCount - 1

            link.Text = "+"
        End If

        Me.ResumeLayout()
    End Sub
End Class

Open in new window

0
CodeCruiserCommented:
Try setting its Visible to false, calling Application.DoEvents and then setting Visible to True at the end.
0
tonelm54Author Commented:
Stops the screen flicering but still takes ages to complete, from about the 6th drop down it takes about 30 seconds to draw, any ideas how I can increase the execution speed?

Thank you
0
CodeCruiserCommented:
That is very strange to be honest. It should not take that long. Add messageboxes to see how many controls you have in the table at that time.
0
tonelm54Author Commented:
The actual line its taking ages on is:-
            parentLayoutPanel.Controls.Add(newTableLayout, 1, pos.Row + 1)

I added the lines:-
            Debug.Print("Will start to draw at " & Now.Second)
            parentLayoutPanel.Controls.Add(newTableLayout, 1, pos.Row + 1)
            Debug.Print("Ended draw at " & Now.Second)

Which the first couple of drop downs shows:-
               Will start to draw at 32
               Ended draw at 56

However during the first the entire sub takes a second, and everything stays the same, until it gets to this line, and then every execution adds a bit onto it.
0
CodeCruiserCommented:
I think its the deep hierarchy of the tablelayouts which causes the problem. Its nothing to do with painting.
0

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
tonelm54Author Commented:
Ok, killed my Virus scan software which has speeded things up, but its still slower than Id like.

The code the Im running to test this is:-
            Debug.Print("Will start to draw tabe " & tableDraw & " at " & Now.Second & "." & Now.Millisecond)
            parentLayoutPanel.Controls.Add(newTableLayout, 1, pos.Row + 1)
            Debug.Print("Ended the table draw " & tableDraw & " at " & Now.Second & "." & Now.Millisecond)
            tableDraw = tableDraw + 1

Which gives me:-
               Will start to draw tabe 0 at 5.253
               Ended the table draw 0 at 5.300
               Will start to draw tabe 1 at 6.214
               Ended the table draw 1 at 6.259
               Will start to draw tabe 2 at 11.313
               Ended the table draw 2 at 11.368
               Will start to draw tabe 3 at 12.819
               Ended the table draw 3 at 12.905
               Will start to draw tabe 4 at 13.954
               Ended the table draw 4 at 14.88
               Will start to draw tabe 5 at 15.98
               Ended the table draw 5 at 15.299
               Will start to draw tabe 6 at 16.509
               Ended the table draw 6 at 16.833
               Will start to draw tabe 7 at 18.416
               Ended the table draw 7 at 18.974
               Will start to draw tabe 8 at 22.428
               Ended the table draw 8 at 23.452
               Will start to draw tabe 9 at 27.730
               Ended the table draw 9 at 29.675
               Will start to draw tabe 10 at 36.880
               Ended the table draw 10 at 40.677
               Will start to draw tabe 11 at 55.792
               Ended the table draw 11 at 3.260
               Will start to draw tabe 12 at 27.733
               Ended the table draw 12 at 41.659

As you can see on the 12th table it takes nearly 15 seconds to draw it :-(
Imports System.Windows.Forms

Public Class Form1
    Dim cellCnt As Integer
    Dim tableDraw As Integer = 0

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        AddHandler LinkLabel1.Click, AddressOf clickButton
        AddHandler LinkLabel2.Click, AddressOf clickButton
        AddHandler LinkLabel3.Click, AddressOf clickButton
        AddHandler LinkLabel4.Click, AddressOf clickButton

        For x = 0 To 4 Step 1
            Dim tmpLabel As New Label
            cellCnt = cellCnt + 1
            tmpLabel.Text = cellCnt
            TableLayoutPanel1.Controls.Add(tmpLabel, 1, x)
        Next



    End Sub

    Private Sub clickButton(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim link As LinkLabel = CType(sender, LinkLabel)

        If link.Text = "+" Then
            link.Text = "-"
            Dim innerTLP As TableLayoutPanel = link.Parent

            Dim pos As TableLayoutPanelCellPosition = innerTLP.GetCellPosition(link)

            For Each ExistControl In innerTLP.Controls
                If innerTLP.GetRow(ExistControl) > pos.Row Then
                    innerTLP.SetRow(ExistControl, innerTLP.GetRow(ExistControl) + 1)
                End If
            Next

            Dim newTableLayout As New TableLayoutPanel
            newTableLayout.RowCount = 4
            newTableLayout.RowStyles.Add(New RowStyle(SizeType.AutoSize))

            newTableLayout.AutoSize = True
            newTableLayout.AutoSizeMode = AutoSizeMode.GrowAndShrink

            newTableLayout.ColumnStyles.Add(New ColumnStyle(SizeType.Absolute, 20))
            newTableLayout.ColumnStyles.Add(New ColumnStyle(SizeType.AutoSize))


            For x = 1 To 4 Step 1
                Dim newLabel1 As New LinkLabel
                If x Mod 2 = 0 Then
                    newLabel1.Text = "-"
                Else
                    newLabel1.Text = "+"
                End If

                cellCnt = cellCnt + 1
                Dim tmpLabel As New Label
                tmpLabel.Text = cellCnt
                newTableLayout.Controls.Add(tmpLabel, 1, x)
                AddHandler newLabel1.Click, AddressOf clickButton
                newTableLayout.Controls.Add(newLabel1, 0, x)
            Next

            Dim parentLayoutPanel As TableLayoutPanel
            parentLayoutPanel = link.Parent
            Debug.Print("Will start to draw tabe " & tableDraw & " at " & Now.Second & "." & Now.Millisecond)
            parentLayoutPanel.Controls.Add(newTableLayout, 1, pos.Row + 1)
            Debug.Print("Ended the table draw " & tableDraw & " at " & Now.Second & "." & Now.Millisecond)
            tableDraw = tableDraw + 1
        ElseIf link.Text = "-" Then
            Dim innerTLP As TableLayoutPanel = link.Parent
            Dim pos As TableLayoutPanelCellPosition = innerTLP.GetCellPosition(link)

            For Each ExistControl In innerTLP.Controls
                If innerTLP.GetRow(ExistControl) = pos.Row + 1 Then
                    innerTLP.Controls.Remove(ExistControl)
                End If
            Next

            For Each ExistControl In innerTLP.Controls
                If innerTLP.GetRow(ExistControl) > pos.Row + 1 Then
                    innerTLP.SetRow(ExistControl, innerTLP.GetRow(ExistControl) - 1)
                End If
            Next
            innerTLP.RowCount = innerTLP.RowCount - 1

            link.Text = "+"
        End If
    End Sub
End Class

Open in new window

0
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.