Van*_*ith 18 .net c# tablelayoutpanel winforms
不要问为什么,但我要求在a中的某些单元格周围绘制边框TableLayoutPanel
.
例如,为简单起见,假设我有1行,5列TableLayoutPanel
.每个单元格中都有一个按钮.我想在前3个单元格周围绘制一个框,然后在最后2个单元格周围绘制另一个框.总共有两个盒子.
有关如何实现这一目标的任何建议?
谢谢.
Ale*_*Aza 28
您可以使用CellPaint
事件并在需要时绘制边框矩形:
tableLayoutPanel1.CellPaint += tableLayoutPanel1_CellPaint;
Run Code Online (Sandbox Code Playgroud)
处理程序:
void tableLayoutPanel1_CellPaint(object sender, TableLayoutCellPaintEventArgs e)
{
if (e.Column == 1 && e.Row == 0)
e.Graphics.DrawRectangle(new Pen(Color.Blue), e.CellBounds);
}
Run Code Online (Sandbox Code Playgroud)
您可以使用ControlPaint
以下方式绘制任何类型的边框
if (e.Column == 1 && e.Row == 0)
{
var rectangle = e.CellBounds;
rectangle.Inflate(-1, -1);
ControlPaint.DrawBorder3D(e.Graphics, rectangle, Border3DStyle.Raised, Border3DSide.All); // 3D border
ControlPaint.DrawBorder(e.Graphics, rectangle, Color.Red, ButtonBorderStyle.Dotted); // dotted border
}
Run Code Online (Sandbox Code Playgroud)