本文讲述了C#关于重绘tableLayoutPanel单元格边框后的问题!具有很好的参考价值,希望对大家有所帮助。一起跟随六星小编过来看看吧,具体如下:
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); }
更多相关技术内容咨询欢迎前往并持续关注六星社区了解详情。
想高效系统的学习Python编程语言,推荐大家关注一个微信公众号:Python编程学习圈。每天分享行业资讯、技术干货供大家阅读,关注即可免费领取整套Python入门到进阶的学习资料以及教程,感兴趣的小伙伴赶紧行动起来吧。
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!