CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Jan 2010
    Posts
    130

    [RESOLVED] Delete row from Table Panel Layout?

    Using the code below I manage to remove all the controls from my rows but the rows themselves dont get deleted... any help?

    Code:
     if (tblPnl.RowCount > 1)
    {
                    for (int i = 1; i < tblPnl.RowCount; i++) // Starting at i=1 so that the first row never gets deleted
                    {
                        tblPnl.Controls.RemoveAt(i);
                    }
    }

  2. #2

    Re: Delete row from Table Panel Layout?

    You have to manually re-creat the entire TableLayoutPanel. For some reason Microsoft in their infinite wisdom never thought that people when removing a row, they actually would want the row to go away.

    Here is a class that I have written that can do this for you.

    Code:
        public class MultiRowPanel
            : Panel
        {
            int suspended = 0;
            public void SuspendTableDraw()
            {
                suspended++;
            }
    
            public void ResumeTableDraw()
            {
                suspended = Math.Max(0, suspended--);
                if (suspended==0)
                    ReDrawTable();
            }
    
            public void ClearRows()
            {
                this.innerctrls.Clear();
                ReDrawTable();
            }
    
            public void RemoveRow(Control c)
            {
                innerctrls.Remove(c);
                ReDrawTable();
            }
    
            public void AddRows(Control[] controls)
            {
                foreach (Control c in controls)
                {
                    if (innerctrls.Contains(c))
                        throw new Exception("The control already exists in the table.");
                    innerctrls.Add(c);
                }
                ReDrawTable();
            }
    
            public void AddRow(Control control)
            {
                if (innerctrls.Contains(control))
                    throw new Exception("The control already exists in the table.");
                this.innerctrls.Add(control);
                ReDrawTable();
            }
    
            List<Control> innerctrls =
                new List<Control>();
    
            System.Windows.Forms.TableLayoutPanel tableLayoutPanel1;
            public void ReDrawTable()
            {
                if (suspended>0)
                    return;
                
    
                if (tableLayoutPanel1!=null)
                    this.Controls.Remove(tableLayoutPanel1);
    
                this.tableLayoutPanel1 = new TableLayoutPanel();
                this.tableLayoutPanel1.ColumnCount = 1;
                this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 100F));
    
                int rowcount = 0;
    
                foreach (Control c in innerctrls)
                {
                    if (!c.Visible)
                        continue ;
                    this.tableLayoutPanel1.Controls.Add(c, 0, rowcount);
                    rowcount++;
                }
    
                float h = 100.0f / (float)rowcount;
                for (int i = 0; i < rowcount; i++)
                    this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, h));
    
                this.tableLayoutPanel1.Dock = System.Windows.Forms.DockStyle.Fill;
                this.tableLayoutPanel1.Name = "tableLayoutPanel1";
                this.tableLayoutPanel1.RowCount = rowcount;
                this.tableLayoutPanel1.TabIndex = 0;
    
                this.Controls.Add(tableLayoutPanel1);
            }
        }
    Last edited by matthewsanford@gmail.com; June 22nd, 2010 at 04:38 PM.

  3. #3
    Join Date
    Jan 2010
    Posts
    130

    Re: Delete row from Table Panel Layout?

    Thank you, that's a GREAT help!

    One question: how do I create a multirowpanel table of multiple columns ensuring that I can delete a particular control or a whole row on command? I tried editing your code and it turned into a huge mess :S

  4. #4

    Re: Delete row from Table Panel Layout?

    This should do it...

    I have not actually run this but it *should* do the job it may require a few minor tweeks. Let me know how it goes

    Code:
           int suspended = 0;
            public void SuspendTableDraw()
            {
                suspended++;
            }
    
            public void ResumeTableDraw()
            {
                suspended = Math.Max(0, suspended--);
                if (suspended == 0)
                    ReDrawTable();
            }
    
            struct ControlPosition
            {
                public ControlPosition(int Row, int Column)
                {
                    this.Row = Row;
                    this.Column = Column;
                }
    
                public int Row ;
                public int Column;
            }
    
            public void Add(Control control, int row, int column)
            {
                if (innercontrls.ContainsKey(control))
                    throw new Exception("The control already exists in the table.");
                innercontrls.Add(control, new ControlPosition(row, column));
                ReDrawTable();
            }
    
            public void Remove(Control c)
            {
                innercontrls.Remove(c);
                ReDrawTable();
            }
    
            Dictionary<Control, ControlPosition> innercontrls = new Dictionary<Control, ControlPosition>();
            System.Windows.Forms.TableLayoutPanel tableLayoutPanel1;
            public void ReDrawTable()
            {
                if (suspended > 0)
                    return;
                if (tableLayoutPanel1 != null)
                    this.Controls.Remove(tableLayoutPanel1);
                this.tableLayoutPanel1 = new TableLayoutPanel();
    
                int rowcount=0, colcount=0;
                foreach (KeyValuePair<Control, ControlPosition> kvp in innercontrls)
                {
                    rowcount = Math.Max(kvp.Value.Row + 1, rowcount);
                    colcount = Math.Max(kvp.Value.Column + 1, colcount);
                }
                this.tableLayoutPanel1.ColumnCount = colcount ;
                
                float w = 100.0f / colcount;
                for (int i=0; i<colcount; i++)
                    this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, w));
    
                foreach (KeyValuePair<Control, ControlPosition> kvp in innercontrls)
                {
                    if (!kvp.Key.Visible)
                        continue;
                    this.tableLayoutPanel1.Controls.Add(kvp.Key, kvp.Value.Row, kvp.Value.Column);
                }
    
                float h = 100.0f / (float)rowcount;
                for (int i = 0; i < rowcount; i++)
                    this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, h));
    
                this.tableLayoutPanel1.Dock = System.Windows.Forms.DockStyle.Fill;
                this.tableLayoutPanel1.Name = "tableLayoutPanel1";
                this.tableLayoutPanel1.RowCount = rowcount;
                this.tableLayoutPanel1.TabIndex = 0;
    
                this.Controls.Add(tableLayoutPanel1);
    
            }

  5. #5
    Join Date
    Jan 2010
    Posts
    130

    Re: Delete row from Table Panel Layout?

    Thanks once again. I have now been able to add many more methods to the multirowpanel class There is however one method I created that keeps returning a null pointer exception when i throw it:

    MultiRowPanel Class:
    Code:
    public void SetBackgrColor(string value)
            {
                tableLayoutPanel1.BackColor = ColorTranslator.FromHtml(value);
            }
    Main Program:
    Code:
           protected void InitializeFormat()
            {
                menuStripSnatcher.BackColor = ColorTranslator.FromHtml("#3A9DCB");
                gbxFormat.ForeColor = ColorTranslator.FromHtml("#3A9DCB");
                tblPnl.SetBackgrColor("#3A9DCB"); // Returns null pointer
    }

  6. #6
    Join Date
    Jan 2010
    Posts
    130

    Talking Re: Delete row from Table Panel Layout?

    No matter, I just set it in the c# class itself

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured