Winforms:带有对接的 FlowLayoutPanel

发布于 2024-11-16 16:06:56 字数 205 浏览 6 评论 0原文

这是winforms里的。我正在创建一个用户控件,它基本上是一个充满其他用户控件的 FlowlayoutControl。我需要添加的每个控件都停靠在前一个控件的顶部(从左到右)。不幸的是,看起来 flowlayoutcontrol 忽略了任何停靠属性。有什么办法可以将控件停靠在那里吗?我需要它从左到右填充项目,但项目应该像列表视图一样布局。我确实无法提供任何代码,因为这需要弄清楚采取什么方法。

This is in winforms. I am creating a User control that is basically a FlowlayoutControl filled with other User Controls. I need each of the controls added to be docked to the top of the previous (from left to right). Unfortunately it looks like the flowlayoutcontrol ignores any of the docking properties. Is there any way to dock the controls inside there? I need it to fill the item from left to right, but the items should be laid out like a list view. Theres really no code i can provide due to the fact that its a matter of figuring out what approach to take.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

慕烟庭风 2024-11-23 16:06:56

让 FlowLayoutPanel 正确停靠是很棘手的。从最初的问题来看,您想要类似列表视图的东西。重要的是要知道列表中的一项(最宽的一项)在 FlowLayoutPanel 中定义了“虚拟列”。其余项目将随之而来。您可以通过将其中一项拖到右侧来在 VS 设计器中证明这一点。 “虚拟列”将跟随它,如果您的其他项目已锚定,它们将跟随虚拟列。

请注意,您无法锚定定义列的控件。它没有任何可以锚定的东西,奇怪的事情就会发生。

请以编程方式完成所有这些操作,处理 FlowLayoutPanel 上的 Layout 事件并放置类似于以下代码的代码。重要的是,在设计器中,列表中的所有项目都不会停靠,并且它们的锚定设置为“无”。我昨天花了一天的时间在这上面,在设计器中这样做是使下面的代码工作的原因。

flowLayoutPanel.Controls[0].Dock = DockStyle.None;                
flowLayoutPanel.Controls[0].Width = flowLayoutPanel.DisplayRectangle.Width - lowLayoutPanel.Controls[0].Margin.Horizontal;

for (int i = 1; i < flowLayoutPanel.Controls.Count; i++)
{
    flowLayoutPanel.Controls[i].Dock = DockStyle.Top;
} 

Getting the FlowLayoutPanel to dock right is tricky. From the original question, you want something like a list view. It's important to know that ONE of the items in your list (the widest one) defines a "virtual column" in the FlowLayoutPanel. The rest of the items will follow it. You can prove this in the VS designer by dragging one of the items to the right. The 'virtual column' will follow it, and if your other items are anchored they will follow the virtual column.

Note that you can't anchor the control that is defining the column. It has nothing to anchor to and strange things will happen.

Do do all this programatically, handle the Layout event on your FlowLayoutPanel and put code similar to the code below. It's important that in the designer all the items in your list are not docked and and have their anchoring set to 'none'. I spent a day on this yesterday and doing that in the designer is what made the code below work.

flowLayoutPanel.Controls[0].Dock = DockStyle.None;                
flowLayoutPanel.Controls[0].Width = flowLayoutPanel.DisplayRectangle.Width - lowLayoutPanel.Controls[0].Margin.Horizontal;

for (int i = 1; i < flowLayoutPanel.Controls.Count; i++)
{
    flowLayoutPanel.Controls[i].Dock = DockStyle.Top;
} 
枕花眠 2024-11-23 16:06:56

FlowLayoutPanel.FlowDirection 属性指示流FlowLayoutPanel 控件的方向。

FlowLayoutPanel.WrapContents 属性指示是否FlowLayoutPanel 控件应该包裹其内容或让内容被剪切。

FlowLayoutPanel.FlowDirection Property indicates the flow direction of the FlowLayoutPanel control.

FlowLayoutPanel.WrapContents Property indicates whether the FlowLayoutPanel control should wrap its contents or let the contents be clipped.

春夜浅 2024-11-23 16:06:56

FlowLayoutPanel 的停靠属性适用于面板本身(例如,如果您希望 FlowLayoutPanel 停靠在窗体的左侧等),而不是其内部控件的容器。

尝试使用 DefaultPadding 和 DefaultMargin 属性,这些属性适用于它包含的控件的间距

The docking properties of the FlowLayoutPanel are for the panel itself (like if you wanted the FlowLayoutPanel docked to the left of the form, etc.), not the container of controls inside of it.

Try playing with the DefaultPadding and DefaultMargin properties, these apply to the spacing of the controls it contains

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文