TableLayoutPanel 常见操作

    技术2022-05-11  34

    'm using the the TableLayoutPanel for a new WinForms 2.0 app and love the way it mimics HTML tables in terms of layout, automatic resizing etc.... But, incredibly, there seems to be no way to dynamically add or delete rows from this container at runtime. Has anyone figured out how to do this? What would be the technical/architectural argument for Microsoft's not exposing Rows and Columns collections in this control that can be managed programmatically? IMO....without this seemingly basic feature, the potential uses for this control diminish greatly. Mike Morris Friday, January 06, 2006 Have you tried MyTableLayoutPanel.Controls.Add(control,colnum,rownum) ? GiorgioG Friday, January 06, 2006 Anything that can be done at design time can also be done at run time. Look at the InitializeComponent method for clues. Turtle Rustler Friday, January 06, 2006 Hi,   to add rows and columns dynamical simply set the column or row size!  You then have to add specific attributes for the columns and rows based using the ColumnStyles and RowStyles properties.  The code below illustrates: table.CellBorderStyle = TableLayoutPanelCellBorderStyle.Single;             table.Width = 210;             table.Height = 210;             table.RowCount = 20;             table.ColumnCount = 20;             for (int i = 0; i < 20; i++)             {                 table.ColumnStyles.Add(new ColumnStyle(SizeType.Absolute, 10));                 table.RowStyles.Add(new RowStyle(SizeType.Absolute, 10));             } Jerod Edward Moemeka Tuesday, January 10, 2006 Hey I just got the "delete" row working.  You have to delete the controls in the row first then shift all the other controls up by one row. Try this to see if it will work for you. FYI I am using the tag to mark the control row id.  Example: protected void DeleteButton_Click(object sender, EventArgs e)         {             //MessageBox.Show("Line item delete pressed. Tag = " + ((Button)sender).Tag);             int iRowId = int.Parse(((Button)sender).Tag.ToString());             this.tlpBillItem.SuspendLayout();             Control c = null;             //delete current row controls             for (int j = 0; j < this.tlpBillItem.ColumnCount; j++)             {                 c = this.tlpBillItem.GetControlFromPosition(j, iRowId);                 if (c != null)                 {                     this.tlpBillItem.Controls.Remove(c);                 }             }             //need to shift all controls up one row from delete point             TableLayoutPanelCellPosition controlPosition;             for (int i = iRowId; i < this.tlpBillItem.RowCount; i++)             {                 for (int j = 0; j < this.tlpBillItem.ColumnCount; j++)                 {                     c = this.tlpBillItem.GetControlFromPosition(j, i + 1);                     if (c == null)                         break;                     c.Tag = i;                     controlPosition = new TableLayoutPanelCellPosition(j, i);                     this.tlpBillItem.SetCellPosition(c, controlPosition);                 }             }             //need to remove the row             this.tlpBillItem.RowCount--;             this.tlpBillItem.ResumeLayout(false);             this.tlpBillItem.PerformLayout();         } Tony Nguyen Friday, January 20, 2006

    最新回复(0)