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); }
得到結果為:
求問題一和問題二出現的原因?