tablelayoutpanel 怎样合并单元格

uncleson88 2012-05-28 08:43:38
我知道控件的RowSpan和ColumnSpan属性和tablelayoutpanel.SetRowSpan(),但是这样只是把控件变大,单元格上的线还在,如果CellBorderStyle = None 当然没事,可如果想要有边框的话就太丑了。
怎样实现真正的单元格合并呢?
...全文
1533 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
uncleson88 2012-05-28
  • 打赏
  • 举报
回复
能教教我怎么反编译吗?.NET 在哪个文件里?
uncleson88 2012-05-28
  • 打赏
  • 举报
回复
谢谢!
uncleson88 2012-05-28
  • 打赏
  • 举报
回复
好晕哦~~~~没搞过啊~~~~您帮帮我
bdmh 2012-05-28
  • 打赏
  • 举报
回复
下载 Reflector.exe 去反编译.net,看看tablelayoutpanel的 OnPaintBackground 代码就清楚了

它里面调用 ControlPaint.PaintTableCellBorder

以下是ControlPaint.PaintTableCellBorder代码

internal static void PaintTableCellBorder(TableLayoutPanelCellBorderStyle borderStyle, Graphics g, Rectangle bound)
{
switch (borderStyle)
{
case TableLayoutPanelCellBorderStyle.None:
return;

case TableLayoutPanelCellBorderStyle.Single:
g.DrawRectangle(SystemPens.ControlDark, bound);
return;

case TableLayoutPanelCellBorderStyle.Inset:
using (Pen pen = new Pen(SystemColors.Window))
{
g.DrawLine(pen, bound.X, bound.Y, (bound.X + bound.Width) - 1, bound.Y);
g.DrawLine(pen, bound.X, bound.Y, bound.X, (bound.Y + bound.Height) - 1);
}
g.DrawLine(SystemPens.ControlDark, (bound.X + bound.Width) - 1, bound.Y, (bound.X + bound.Width) - 1, (bound.Y + bound.Height) - 1);
g.DrawLine(SystemPens.ControlDark, bound.X, (bound.Y + bound.Height) - 1, (bound.X + bound.Width) - 1, (bound.Y + bound.Height) - 1);
return;

case TableLayoutPanelCellBorderStyle.InsetDouble:
g.DrawRectangle(SystemPens.Control, bound);
bound = new Rectangle(bound.X + 1, bound.Y + 1, bound.Width - 1, bound.Height - 1);
using (Pen pen2 = new Pen(SystemColors.Window))
{
g.DrawLine(pen2, bound.X, bound.Y, (bound.X + bound.Width) - 1, bound.Y);
g.DrawLine(pen2, bound.X, bound.Y, bound.X, (bound.Y + bound.Height) - 1);
}
g.DrawLine(SystemPens.ControlDark, (bound.X + bound.Width) - 1, bound.Y, (bound.X + bound.Width) - 1, (bound.Y + bound.Height) - 1);
g.DrawLine(SystemPens.ControlDark, bound.X, (bound.Y + bound.Height) - 1, (bound.X + bound.Width) - 1, (bound.Y + bound.Height) - 1);
return;

case TableLayoutPanelCellBorderStyle.Outset:
{
g.DrawLine(SystemPens.ControlDark, bound.X, bound.Y, (bound.X + bound.Width) - 1, bound.Y);
g.DrawLine(SystemPens.ControlDark, bound.X, bound.Y, bound.X, (bound.Y + bound.Height) - 1);
using (Pen pen3 = new Pen(SystemColors.Window))
{
g.DrawLine(pen3, (bound.X + bound.Width) - 1, bound.Y, (bound.X + bound.Width) - 1, (bound.Y + bound.Height) - 1);
g.DrawLine(pen3, bound.X, (bound.Y + bound.Height) - 1, (bound.X + bound.Width) - 1, (bound.Y + bound.Height) - 1);
return;
}
}
case TableLayoutPanelCellBorderStyle.OutsetDouble:
case TableLayoutPanelCellBorderStyle.OutsetPartial:
g.DrawRectangle(SystemPens.Control, bound);
bound = new Rectangle(bound.X + 1, bound.Y + 1, bound.Width - 1, bound.Height - 1);
g.DrawLine(SystemPens.ControlDark, bound.X, bound.Y, (bound.X + bound.Width) - 1, bound.Y);
g.DrawLine(SystemPens.ControlDark, bound.X, bound.Y, bound.X, (bound.Y + bound.Height) - 1);
using (Pen pen4 = new Pen(SystemColors.Window))
{
g.DrawLine(pen4, (bound.X + bound.Width) - 1, bound.Y, (bound.X + bound.Width) - 1, (bound.Y + bound.Height) - 1);
g.DrawLine(pen4, bound.X, (bound.Y + bound.Height) - 1, (bound.X + bound.Width) - 1, (bound.Y + bound.Height) - 1);
}
return;

default:
return;
}
}



uncleson88 2012-05-28
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]
自己继承一个,重载 OnPaintBackground 方法,在里面处理
[/Quote]

Single的边框我还能写出来,如果是其他的有3D效果的。。。能给点方法吗?
花痴 2012-05-28
  • 打赏
  • 举报
回复
边框可以不使用tablelayoutpanel的边框线,你在需要显示边框的时候,可以在一个单元格内放入一个Panel等容器,使用容易的边框线就可以了喔
bdmh 2012-05-28
  • 打赏
  • 举报
回复
自己继承一个,重载 OnPaintBackground 方法,在里面处理
资源下载链接为: https://pan.quark.cn/s/d9ef5828b597 在C# Windows Forms开发中,TableLayoutPanel控件是一种强大的布局管理工具,广泛用于以表格形式组织其他控件。它支持动态管理功能,如动态增加或删除列、合并单元格以及动态添加控件,这些功能对于创建可自定义或数据驱动的用户界面非常关键。 TableLayoutPanel默认创建时通常只有预设的列数,但可以通过编程方式动态调整列的数量。例如,要增加一列,可以使用ColumnStyles集合的Add方法: 删除列则可以通过指定索引访问ColumnStyles集合并调用RemoveAt方法。例如,删除第一列可以这样实现: 在TableLayoutPanel中,可以通过设置控件的RowSpan和ColumnSpan属性来合并行和列。例如,如果希望一个控件占据两行两列,可以这样设置: 动态添加控件到TableLayoutPanel是常见的需求。这可以通过Controls集合的Add方法实现。例如,假设需要将一个按钮添加到第一行第二列,可以这样操作: 在Visual Studio中,.sln文件是解决方案文件,它包含项目的信息和依赖关系。.suo文件则存储了用户特定的Visual Studio选项。而项目文件夹(如WindowsFormsApplication1)通常包含实际的项目源代码,其中包括实现上述动态管理功能的C#代码。 通过动态管理TableLayoutPanel,开发者可以构建更灵活的用户界面。动态增加和删除列可以适应不同的场景需求;合并单元格可以实现复杂的布局效果;而动态添加控件则可以根据程序运行时的需求创建或更新用户界面。这些技巧对于需要高度定制用户界面的应用程序来说尤为重要。

111,119

社区成员

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

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

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