TableLayoutPanel单元格宽度控制的问题!

超凡 2012-02-29 03:43:12

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的单元格宽度一制!


昨天压宝赚一百分,当散分了!
...全文
821 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
超凡 2012-02-29
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 wxm3630478 的回复:]
那几不获取行列的时候出了问题,这个代码要 先把行、列创建好,在通过计算TablePanel的宽高 和 行列数算平均值.

索引越界....这种错误一调试就明白的

你先到一个测试界面测试一下,创建5行5列,然后把SetGridSize() 放到Form_Load()方法里 试试
[/Quote]

试过了,只能拖控件上去设好列数和行数了才能使用你这个方法!

动态窗体或再添加行或列就不行!
超凡 2012-02-29
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 wxm3630478 的回复:]
那几不获取行列的时候出了问题,这个代码要 先把行、列创建好,在通过计算TablePanel的宽高 和 行列数算平均值.

索引越界....这种错误一调试就明白的

你先到一个测试界面测试一下,创建5行5列,然后把SetGridSize() 放到Form_Load()方法里 试试
[/Quote]

试过了! 在届面上创建tableLayoutPanel 是可以的! 我估计是动态创建,动态添加行列所以读不到RowStyles 连0都超出索引!
wxm3630478 2012-02-29
  • 打赏
  • 举报
回复
那几不获取行列的时候出了问题,这个代码要 先把行、列创建好,在通过计算TablePanel的宽高 和 行列数算平均值.

索引越界....这种错误一调试就明白的

你先到一个测试界面测试一下,创建5行5列,然后把SetGridSize() 放到Form_Load()方法里 试试
超凡 2012-02-29
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 wxm3630478 的回复:]
C# code


/// <summary>
/// 设置网格大小
/// </summary>
public virtual void SetGridSize()
{
int width = this.tableLayoutPanel1.Width; //宽度
int height = this.tableLay……
[/Quote]

抛错,在这一步 RowStyle rs = tableLayoutPanel1.RowStyles[i]; 索引超出范围!
超凡 2012-02-29
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 wxm3630478 的回复:]
C# code


/// <summary>
/// 设置网格大小
/// </summary>
public virtual void SetGridSize()
{
int width = this.tableLayoutPanel1.Width; //宽度
int height = this.tableLay……
[/Quote]

谢了! 我试试!
超凡 2012-02-29
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 bdmh 的回复:]
你就别设置比例了,三等分宽度
[/Quote]

没有设置比例! ... 我是在后台动态生成的控件!比例可能是自己生成的!
wxm3630478 2012-02-29
  • 打赏
  • 举报
回复

/// <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;
}
}
}
超凡 2012-02-29
  • 打赏
  • 举报
回复
winform 的问题!
bdmh 2012-02-29
  • 打赏
  • 举报
回复
你就别设置比例了,三等分宽度

110,499

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧