none
Remove Control From TableLayoutPanel

    Question

  • Hi All,

    I facing problem in TableLayoutPanel. I adding and removing the control dynamicall using TableLauoutPanel. My problem is, if i remove the control in the middle (ie i added TextBox1, TextBox2 and TextBox3 and now i removed TextBox2) at this time other controls are not resized properlly. When the last control (ie TextBox3 ) is removed, its resizing properlly. I attached my code here. What is the problem?



    private
    void AddText_Click(object sender, EventArgs e)
    {

     

    TextBox txt = new TextBox();

    txt.MouseDoubleClick +=

    new MouseEventHandler(txt_MouseDoubleClick);

    txt.Multiline =

    true;

    txt.Dock =

    DockStyle.Fill; NEWADD(txt);

    }


     

    private void NEWADD(Control ctrl)

    {

    tbl.RowCount += 1;

    tbl.RowStyles.Add(

    new RowStyle(SizeType.Percent, 100F / tbl.RowStyles.Count));

    tbl.Controls.Add(ctrl, 0, tbl.RowStyles.Count - 1);

     

    foreach (RowStyle row in tbl.RowStyles)

    {

    row.SizeType =

    SizeType.Percent;

    row.Height = 100F / (tbl.RowStyles.Count);

    }

    }

     



     

    private void RemoveControl(Control ctrl)

    {

    tbl.RowCount -= 1;

    tbl.Controls.Remove(ctrl);

    tbl.RowStyles.Clear();

     

    for (int i = 0; i < tbl.Controls.Count; i++)

    {

    tbl.RowStyles.Add(

    new RowStyle(SizeType.Percent, 100F /( tbl.RowStyles.Count +1)));

    }

     

    foreach (RowStyle row in tbl.RowStyles)

    {

    row.SizeType =

    SizeType.Percent;

    row.Height = 100F / (tbl.RowStyles.Count);

    }

    tbl.Refresh();

     

    }

     



     

    void txt_MouseDoubleClick(object sender, MouseEventArgs e)

    {

    RemoveControl(sender

    as Control);

    }

     



    Mohanavel
    Friday, April 03, 2009 1:01 PM

Answers

  • Hi Mohanavel,

    I performed a test on your sample code and did reproduce the problem on my side. After doing some research, I found the reason that caused the problem, i.e. the controls are not rearranged in the remaining rows in the TableLayoutPanel. Modify the RemoveControl as follows and the problem is solved:

    private void RemoveControl(Control ctrl)
    {

                tbl.RowCount -= 1;           
                tbl.Controls.Remove(ctrl);
               
                tbl.RowStyles.Clear();
              
                for (int i = 0; i < tbl.Controls.Count; i++)
                {

                    tbl.RowStyles.Add(new RowStyle(SizeType.Percent, 100F / (tbl.RowStyles.Count + 1)));
                }

                foreach (RowStyle row in tbl.RowStyles)
                {

                    row.SizeType = SizeType.Percent;

                    row.Height = 100F / (tbl.RowStyles.Count);

                }
                // rearrange the controls in the remaining rows
                int rowindex = 0;
                foreach (Control ctl in tbl.Controls)
                {
                    tbl.SetRow(ctl,rowindex++);
                }

                tbl.Refresh();
      }

    Hope this helps.
    If you have any question, please feel free to let me know.

    Sincerely,
    Linda Liu

    • Marked as answer by Linda Liu Friday, April 10, 2009 2:56 AM
    Thursday, April 09, 2009 2:03 PM

All replies

  • Hi Mohanavel,

    I performed a test on your sample code and did reproduce the problem on my side. After doing some research, I found the reason that caused the problem, i.e. the controls are not rearranged in the remaining rows in the TableLayoutPanel. Modify the RemoveControl as follows and the problem is solved:

    private void RemoveControl(Control ctrl)
    {

                tbl.RowCount -= 1;           
                tbl.Controls.Remove(ctrl);
               
                tbl.RowStyles.Clear();
              
                for (int i = 0; i < tbl.Controls.Count; i++)
                {

                    tbl.RowStyles.Add(new RowStyle(SizeType.Percent, 100F / (tbl.RowStyles.Count + 1)));
                }

                foreach (RowStyle row in tbl.RowStyles)
                {

                    row.SizeType = SizeType.Percent;

                    row.Height = 100F / (tbl.RowStyles.Count);

                }
                // rearrange the controls in the remaining rows
                int rowindex = 0;
                foreach (Control ctl in tbl.Controls)
                {
                    tbl.SetRow(ctl,rowindex++);
                }

                tbl.Refresh();
      }

    Hope this helps.
    If you have any question, please feel free to let me know.

    Sincerely,
    Linda Liu

    • Marked as answer by Linda Liu Friday, April 10, 2009 2:56 AM
    Thursday, April 09, 2009 2:03 PM

  • Ho


      Thanks Linda. I done this by Removing all the control that are added to the ControlCollection and particular control is removed from the ControlCollection. Again all the controls in the collection are added again. This is working as i expected, still i facing the flickering problem. I hope your answer will sove.
    Mohanavel
    Friday, April 10, 2009 9:19 AM