WinForms UserControl 布局自动调整大小问题
不久前,我发布了这个问题,关于尝试让 flowlayoutpanel 在以下情况下正确自动调整大小:停靠在容器的顶部。我试图完成的是设置一个横跨表单顶部边缘的水平(从左到右)flowlayoutpanel,以及一个 datagridview 占据 flowlayoutpanel 下面的剩余可用空间。我无法让 flowlayoutpanel 的 height 属性按其应有的方式自动调整大小。发布的答案非常适合我想要完成的任务,这太棒了...
无法足够好地独自一人,我现在正尝试更进一步,将 flowlayoutpanel 包装在 UserControl 中。然而,我发现这样做会导致 flowlayoutpanel 不再正确调整其高度。
这是第一个布局,效果非常好:
FlowLayoutPanel1 (Autosize = true, Dock = Top)
ComboBox1
ComboBox2
ComboBox3
DataGridView1 (Dock = Fill)
这里是包含在 UserControl 中的 flowlayoutpanel:
UserControl (Autosize = true, Dock = Top)
FlowLayoutPanel1 (Autosizse = true, Dock = Fill)
ComboBox1
ComboBox2
ComboBox3
DataGridView1 (Dock = Fill)
无论出于何种原因,当调整表单大小时,flowlayoutpanel 都不会正确调整其高度。这很容易重现。哦还有,FlowLayoutPanel 的内容可以是任何内容,而不仅仅是 ComboBox。
A while back, I posted this question about trying to get a flowlayoutpanel to autosize properly when docked to the top of a container. What I was trying accomplish was to setup a horizontal (left-to-right) flowlayoutpanel spanning across the top edge of a form, and a datagridview taking up the rest of the available space below the flowlayoutpanel. I was having trouble getting the flowlayoutpanel's height property to autosize the way it should. The posted answer worked perfectly for what I was trying to accomplish, which was great...
Unable to leave well enough alone, I am now trying to go one step further and wrap the flowlayoutpanel in a UserControl. I've discovered, however, that doing this causes the flowlayoutpanel to no longer resize its height properly.
Here's the first layout, which works beautifully:
FlowLayoutPanel1 (Autosize = true, Dock = Top)
ComboBox1
ComboBox2
ComboBox3
DataGridView1 (Dock = Fill)
And here it is with flowlayoutpanel wrapped in a UserControl:
UserControl (Autosize = true, Dock = Top)
FlowLayoutPanel1 (Autosizse = true, Dock = Fill)
ComboBox1
ComboBox2
ComboBox3
DataGridView1 (Dock = Fill)
For whatever reason, flowlayoutpanel doesn't resize its height properly when the form is resized. This is pretty easy to reproduce. Oh also, the FlowLayoutPanel's contents can be anything, not just ComboBoxes.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为问题是你有
FlowLayoutPanel1.Dock = Fill
它覆盖了它自动调整高度的尝试。解决方法可能是设置
FlowLayoutPanel1.Dock = Top
(如您之前所做的那样),然后处理FlowLayoutPanel1.Resize
事件以调整用户控件的大小。本质上,您手动尝试使UserControl.Size
与FlowLayoutPanel1.Size
保持同步。可能有一种更聪明的方法来做到这一点,但希望它能让您走上正确的道路......
I think what the problem is is that the you have
FlowLayoutPanel1.Dock = Fill
which overrides it's attempt to autosize it's height.The fix is probably to set
FlowLayoutPanel1.Dock = Top
(as you had it before), and then handle theFlowLayoutPanel1.Resize
event to resize your user control. Essentially, you're manually trying to keep theUserControl.Size
in sync with theFlowLayoutPanel1.Size
.There could be a smarter way to do that, but hopefully it will get you on the right path...
您的 UserControl 设置了宽度吗?请查看 Designer.cs 进行确定。如果是这样,您需要重置该属性。
Does your UserControl have a width set? Look in the designer.cs to be sure. If it does, you'll need to reset that property.