public void set_ControlsColl()
 {
           //gouboxItem这是一个groupbox控件
           TableLayoutPanel tabPanel = new TableLayoutPanel();
            //起始值设为三个单元格
            tabPanel.ColumnCount = 3;
            this.gouboxItem.Controls.Add(tabPanel);
            tabPanel.CellPaint += new TableLayoutCellPaintEventHandler(tabPanel_CellPaint);
            tabPanel.Dock = DockStyle.Fill;
            btn_tableControl();
 } /// <summary>
        /// 重绘单元格颜色
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void tabPanel_CellPaint(object sender, TableLayoutCellPaintEventArgs e)
        {
            Pen pp = new Pen(Color.White);
            e.Graphics.DrawRectangle(pp, e.CellBounds.X, e.CellBounds.Y, e.CellBounds.Width,
            e.CellBounds.Height); 
        }
  public void btn_tableControl()
        {
            int iClickedTimes = 0;            TableLayoutPanel tabPanel = (TableLayoutPanel)(this.gouboxItem.Controls[0]);
            tabPanel.ColumnCount = (iClickedTimes + 1) * 3;  
           // tabPanel.AutoSize = true;            //增加button1
            Button btn1 = new Button();
            btn1.Name = "button" + (iClickedTimes * 3 + 1).ToString();
            btn1.Text = "buttonText" + (iClickedTimes * 3 + 1).ToString();
            tabPanel.Controls.Add(btn1, 0, iClickedTimes);
            //tabPanel.SetRowSpan(btn1, 2);//跨两行            //增加button2
            Button btn2 = new Button();
            btn2.Name = "button" + (iClickedTimes * 3 + 2).ToString();
            btn2.Text = "buttonText" + (iClickedTimes * 3 + 2).ToString();
            tabPanel.Controls.Add(btn2, 1, iClickedTimes);            //增加button3
            Button btn3 = new Button();
            btn3.Name = "button" + (iClickedTimes * 3 + 3).ToString();
            btn3.Text = "buttonText" + (iClickedTimes * 3 + 3).ToString();
            tabPanel.Controls.Add(btn3, 1, iClickedTimes);        }
以上是我代码问题是我有两个单元格的宽度是一制的,还一个单元格宽度是自动缩放的,宽度比较大! 请问大家怎么设置才能使TableLayoutPanel的单元格宽度一制! 
昨天压宝赚一百分,当散分了!

解决方案 »

  1.   


        /// <summary>
        /// 设置网格大小
        /// </summary>
        public virtual void SetGridSize()
        {
          int width = this.tableLayoutPanel1.Width; //宽度
          int height = this.tableLayoutPanel1.Height;       //高度      //行数列数
          int rowcount = this.tableLayoutPanel1.RowCount;
          int columncount = this.tableLayoutPanel1.ColumnCount;      if (this.tableLayoutPanel1.CellBorderStyle == TableLayoutPanelCellBorderStyle.Single)
          {
            width -= (columncount + 1);  //减去条线的宽度
            height -= (rowcount + 1);
          }      //获取余数
          int rowremainder = height % rowcount; //余数
          int colremainder = width % columncount;      //AVG
          int rowheight = height / rowcount;
          int columnwidth = width / columncount;      //设置行高
          for (int i = 0; i < rowcount; i++)
          {
            RowStyle rs = tableLayoutPanel1.RowStyles[i];
            rs.SizeType = SizeType.Absolute;
            if (i < rowremainder)
            {
              rs.Height = rowheight + 1; //把多余的数平均一下,用float类型计算会有点问题,所以干脆用int
            }
            else
            {
              rs.Height = rowheight;
            }
          }      //设置列宽
          for (int j = 0; j < columncount; j++)
          {
            ColumnStyle cs = tableLayoutPanel1.ColumnStyles[j];
            cs.SizeType = SizeType.Absolute;
            if (j < colremainder)
            {
              cs.Width = columnwidth + 1;
            }
            else
            {
              cs.Width = columnwidth;
            }
          }
        }
      

  2.   


    抛错,在这一步 RowStyle rs = tableLayoutPanel1.RowStyles[i];  索引超出范围!
      

  3.   

    那几不获取行列的时候出了问题,这个代码要 先把行、列创建好,在通过计算TablePanel的宽高 和 行列数算平均值.索引越界....这种错误一调试就明白的你先到一个测试界面测试一下,创建5行5列,然后把SetGridSize() 放到Form_Load()方法里 试试
      

  4.   


    试过了! 在届面上创建tableLayoutPanel 是可以的! 我估计是动态创建,动态添加行列所以读不到RowStyles 连0都超出索引!