我正尝试使用自己的布局引擎创建自定义面板控件。 我需要添加到我的面板的下方添加,并采取全宽(-padding),像下面每一个控制: 带布局引擎的自定义面板
下面是我的代码:
using System.Drawing;
using System.Windows.Forms;
using System.Windows.Forms.Layout;
namespace VContainer
{
internal class VerticalFillList : Panel
{
public VerticalFillList()
{
AutoScroll = true;
MinimumSize = new Size(200, 200);
Size = new Size(200, 300);
Padding = new Padding(10);
}
private readonly VerticalFillLayout _layoutEngine = new VerticalFillLayout();
public override LayoutEngine LayoutEngine
{
get { return _layoutEngine; }
}
private int _space = 10;
public int Space
{
get { return _space; }
set
{
_space = value;
Invalidate();
}
}
}
internal class VerticalFillLayout : LayoutEngine
{
public override bool Layout(object container, LayoutEventArgs layoutEventArgs)
{
var parent = container as VerticalFillList;
Rectangle parentDisplayRectangle = parent.DisplayRectangle;
Point nextControlLocation = parentDisplayRectangle.Location;
foreach (Control c in parent.Controls)
{
if (!c.Visible)
{
continue;
}
c.Location = nextControlLocation;
c.Width = parentDisplayRectangle.Width;
nextControlLocation.Offset(0, c.Height + parent.Space);
}
return false;
}
}
}
上面的代码工作正常,除了一件事: 当我添加控件到我的容器他们正确地添加(新的父母,100%宽度),但当控件的高度大于我的容器高度我得到水平滚动条,但添加一些控件更多的滚动条被删除。
同样的事情发生时,我想调整我的容器:
这可怎么固定?我只需要移除水平滚动条。
当然,所有的改进都是欢迎的。
我不想使用表格布局或流式布局,因为这一个给我什么时候我需要的。
我需要一个简单的容器,从上到下对所有子控件进行排序并水平拉伸它们,因此它们需要尽可能多的宽度,因此不需要容器水平滚动条。
button1.Anchor = AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top;
因此他们将调整水平
但如果它是固定的,你不能把新闻bottons永远... O_O – MayogaX 2013-03-12 11:43:17
@MayogaX - 我知道:)我只是想表达我的观点。我将有自定义控件而不是按钮。我的观点是要展示我的面板如何运作。大多数情况下,我会在那里有2-3个控件,但是当用户添加新控件时,我想避免当水平滚动条可见时出现的情况。调整我的应用程序的同样的情况。我将在那里持有最多5-6个控件。以上只是一个演示,以显示不需要的行为。 – Misiu 2013-03-12 11:48:35
您正试图解决Winforms设计师在LayoutEngine类中小心翼翼的问题。像这样的布局*双稳态*。试图解决它的最典型的结果是在水平滚动条闪烁的情况下陷入无限循环。你有点注定要重塑班级,并最终以同样的方式去做。 – 2013-03-12 11:50:02