1.用label填充tableLayoutPanel1单元格,label的属性设置为:
anchor:Top, Bottom, Left, Right;
Dock:Fill;
margin:1,1,1,1
Rowspan:2;//根据需要
TextAline:MiddleCenter;
其他属性默认
tableLayoutPanel1的属性默认
得到的结果为:
为了显示表格边框线,在tableLayoutPanel1_CellPaint事件中绘制单元格边框:
private void tableLayoutPanel1_CellPaint(object sender, TableLayoutCellPaintEventArgs e) { //绘制单元格边框 Pen cpen = new Pen(Color.Black); cpen.Width = 1F; Rectangle rectangle = new Rectangle(e.CellBounds.X, e.CellBounds.Y, e.CellBounds.X + this.Width, e.CellBounds.Y + this.Height); e.Graphics.DrawRectangle(cpen,rectangle); }
得到的结果为(tableLayoutPanel1的底边和右边边框线不显示,这是问题一):
补救办法一,在tableLayoutPanel1的四个边框处绘制矩形:
private void SplitContainer1_Panel1_Paint(object sender, PaintEventArgs e) {//绘制表格边框 Pen tpen = new Pen(Color.Black); tpen.Width = 1F; int x = tableLayoutPanel1.Bounds.X; int y = tableLayoutPanel1.Bounds.Y; int width = tableLayoutPanel1.Width; int height = tableLayoutPanel1.Height; //绘制矩形 Rectangle rectangle = new Rectangle(x, y, x + width, y + height); e.Graphics.DrawRectangle(tpen, rectangle); }
得到的结果为:tableLayoutPanel1右边框和下边框和单元格之间有缝隙,这是问题二(tableLayoutPanel1放在SplitContainer1_Panel1内):
补救办法二,单独绘制tableLayoutPanel1的下边框和右边框:
private void SplitContainer1_Panel1_Paint(object sender, PaintEventArgs e) {//绘制表格右、下边框 Pen tpen = new Pen(Color.Black); tpen.Width = 1F; int x = tableLayoutPanel1.Bounds.X; int y = tableLayoutPanel1.Bounds.Y; int width = tableLayoutPanel1.Width; int height = tableLayoutPanel1.Height; //绘制矩形 //Rectangle rectangle = new Rectangle(x, y, x + width, y + height); //e.Graphics.DrawRectangle(tpen, rectangle); //绘制底边 e.Graphics.DrawLine(tpen, x, y + height, x+width, y + height); //绘制右边 e.Graphics.DrawLine(tpen, x + width, y, x + width, y + height); }
得到结果为:
求问题一和问题二出现的原因?