tableLayoutPanel: 表格布局面板

 tableLayoutPanel: 表格布局面板,适合以表格形式规则的动态添加(显示)控件。使用方法概述:将 tableLayoutPanel 拖放到窗体指定区域 ——一般做些基本的设置 或添加行或列,其它的就需写代码来控制显示         ///


        /// 绘制控件
        ///

        private void Paintcontrol()
        {
            // 删除默认的行和列样式 
            tableLayoutPanel1.ColumnStyles.Clear();   
            tableLayoutPanel1.RowStyles.Clear();
            this.tableLayoutPanel1.Controls.Clear();

            this.tableLayoutPanel1.ColumnCount = 4;
            
            Label lb = null;

            if (tableitems != null && tableitems.Rows.Count > 0)
            {
                this.tableLayoutPanel1.RowCount = (tableitems.Rows.Count % 2 == 0 ? tableitems.Rows.Count /2 : tableitems.Rows.Count /2 + 1);
                this.tableLayoutPanel1.Height = 30 * this.tableLayoutPanel1.RowCount;
                this.Height = this.tableLayoutPanel1.Height;

                for (int RowIndex = 0; RowIndex < tableitems.Rows.Count; RowIndex++)
                {         
                    lb = new Label();
                    lb.Text = tableitems.Rows[RowIndex]["ItemText"].ToString();
                    lb.Parent = tableLayoutPanel1;

                    this.tableLayoutPanel1.Controls.Add(lb, RowIndex%2*2, RowIndex/2);

                    TextBox text = new TextBox();
                    text.Name = "txt" + tableitems.Rows[RowIndex]["ItemValue"].ToString();
                    text.Width = 148;
                    text.Parent = tableLayoutPanel1;
                    this.tableLayoutPanel1.Controls.Add(text, RowIndex%2*2+1, RowIndex/2);                
                        
                }
            }
      
        }

 

 

 

 

 

 

 

 

 

 

 

你可能感兴趣的:(编程世界)