0

I have a table layout panel that I am dynamically adding rows to using the following code:

    attemptstlp.RowCount += 2
    attemptstlp.Height = attemptstlp.Height + 62
    attemptstlp.RowStyles.Add(New RowStyle(SizeType.Absolute, 30))

(just so you know attemptstlp is the name of the panel)

I am using a loop to process through these rows adding them. I am finding that all is working except half way through the row style stops applying (so if i want to add 24 lots of 2 rows the height will stop applying after the 12th lot of rows has been added).

Could anyone offer suggestions on why the rows are reverting (i assume) to auto size after half of them have been added. The only other lines of code that refer to this panel is the lines adding the text boxes and the lines to suspend and resume layout to help reduce the flickering and time taken to load.

The table layout panel has an inital height of 40 with 1 row of height 39 when first created.

Thanks in advance, mrtechguy

Sathish
  • 4,419
  • 4
  • 30
  • 59
mrtechguy
  • 35
  • 8
  • table layout panel is dock fill the form ah? – Sathish Jul 19 '14 at 09:40
  • Apologies, the formatting didn't work quite as I planned it in my browser (something to do with a pasted space being not a actual space or something) @Sathish – mrtechguy Jul 19 '14 at 10:39
  • its work or do you have problem in table layout panel – Sathish Jul 19 '14 at 10:49
  • The row height of the table layout panel is being ignored for the second half of the total number of rows added. – mrtechguy Jul 19 '14 at 11:08
  • 1
    you are adding `attemptstlp.RowCount += 2` but add one row why `attemptstlp.RowStyles.Add(New RowStyle(SizeType.Absolute, 30))` – Sathish Jul 19 '14 at 11:11
  • I am adding two rows on each loop through, should I be adding them one at a time and adding the style to each one after that? – mrtechguy Jul 19 '14 at 11:49
  • I found this post helpful when dynamically adding TableLayoutPanel rows: http://stackoverflow.com/questions/1142873/winforms-tablelayoutpanel-adding-rows-programatically – CtrlDot Jul 19 '14 at 17:38

1 Answers1

0

This works perfectly for adding rows and controls in a TableLayoutPanel. Try and see.

'Define a blank Tablelayoutpanel with 3 columns in the design page
Dim TableLayoutPanel3 As New TableLayoutPanel()
TableLayoutPanel3.Name = "TableLayoutPanel3"
TableLayoutPanel3.Location = New System.Drawing.Point(32, 287)
TableLayoutPanel3.AutoSize = True
TableLayoutPanel3.Size = New System.Drawing.Size(620, 20)
TableLayoutPanel3.ColumnCount = 3
TableLayoutPanel3.CellBorderStyle = TableLayoutPanelCellBorderStyle.Single
TableLayoutPanel3.BackColor = System.Drawing.Color.Transparent
TableLayoutPanel3.ColumnStyles.Add(New ColumnStyle(SizeType.Percent, 26.34146!))
TableLayoutPanel3.ColumnStyles.Add(New ColumnStyle(SizeType.Percent, 73.65854!))
TableLayoutPanel3.ColumnStyles.Add(New ColumnStyle(SizeType.Absolute, 85.0!))
Controls.Add(TableLayoutPanel3)  

'Create a button btnAddRow to add rows on each click
Private Sub btnAddRow_Click(sender As System.Object, e As System.EventArgs) Handles btnAddRow.Click      
     TableLayoutPanel3.GrowStyle = TableLayoutPanelGrowStyle.AddRows
     TableLayoutPanel3.RowStyles.Add(New RowStyle(SizeType.Absolute, 20))
     TableLayoutPanel3.SuspendLayout()
     TableLayoutPanel3.RowCount += 1         
     Dim tb1 As New TextBox()
     Dim tb2 As New TextBox()
     Dim tb3 As New TextBox()
     TableLayoutPanel3.Controls.Add(tb1 , 0, TableLayoutPanel3.RowCount - 1)
     TableLayoutPanel3.Controls.Add(tb2, 1, TableLayoutPanel3.RowCount - 1)
     TableLayoutPanel3.Controls.Add(tb3, 2, TableLayoutPanel3.RowCount - 1)
     TableLayoutPanel3.ResumeLayout()
     tb1.Focus()
End Sub
EIV
  • 399
  • 3
  • 8