Question

I have a tablelayout panel in which I am adding rows programmatically. When Adding rows to this container the first time the form is opened (using statement with new ValidationForm()) the table displays fine. Upon the second time, I get added rows at the top for some reason? See the picture here:

Extra Row lines at the top

This does not affect the number of rows that I use, just displays extra lines. And the number of lines increases each time i close and open the form. Here is the code i use to add rows:

private void insertRow(Label label, dynamic control)
    {
        // Create Panel
        var panel = new Panel();
        // Set Object Properties
        label.TextAlign = ContentAlignment.MiddleCenter;
        label.Dock = DockStyle.Fill;
        control.Dock = DockStyle.Fill;
        panel.Dock = DockStyle.Fill;

        //generate delete button
        var delete = new Button();
        delete.Text = "X";
        delete.ForeColor = System.Drawing.Color.Red;
        delete.MaximumSize.Height.Equals(40);
        delete.Name = rowCount.ToString();
        delete.Dock = DockStyle.Right;
        delete.Click += new EventHandler(deleteRow);

        // Add Controls to the panel
        panel.Controls.Add(control);
        panel.Controls.Add(delete);
        // add controls
        //tableLayoutPanel_Validations.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 48F));
        tableLayoutPanel_Validations.RowCount = rowCount + 1;
        tableLayoutPanel_Validations.Controls.Add(label, 0, rowCount);
        tableLayoutPanel_Validations.Controls.Add(panel, 1, rowCount);
        tableLayoutPanel_Validations.RowStyles.Add(new RowStyle(SizeType.AutoSize));
        rowCount++;
    }

rowCount is a private int that is set to 0 at runtime.

My first instinct is that the entire form would "reset" after the using statement. The form declaration is public partial class ValidationForm: Form {}

Using statement opening the form:

using (AVBuilder.ValidationForm validationBuilder = new AVBuilder.ValidationForm())
        {
            if (validationBuilder.ShowDialog() == DialogResult.OK)
            {
                // ADD ITEM TO Validations LIST
                ListViewItem result = new ListViewItem(validationBuilder.resultObject + " : " + validationBuilder.resultName);
                result.SubItems.Add(validationBuilder.resultJson);
                result.ToolTipText = result.Text;
                listView_Validations.Items.Add(result);
                //MessageBox.Show(validationBuilder.resultJson);
            }
        }
Was it helpful?

Solution

Make sure the rowCount variable is not declared static, as it will not reset to 0 on a new form instance.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top