【发布时间】:2014-09-10 18:47:01
【问题描述】:
我正在尝试创建一个StackPanel 用户控件,它允许我以垂直方式堆叠新添加的控件(如 WPF StackPanel,但使用 Windows 窗体)。
我使用下面显示的核心代码创建了一个新的用户控件,但即使我的 Location 在设计器中获得了正确的值,我也可以弹出消息框,但设计器视图不会重新排列我拖放到的子控件我的堆栈面板,也在运行时我的位置似乎被设计师认为正确的内容覆盖(我放置控件的位置)
Public Class StackPanel
Inherits Panel
Private biasHeight As Integer = 0
Private Sub StackPanel_ControlAdded(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ControlEventArgs) Handles MyBase.ControlAdded
Dim newControl As System.Windows.Forms.Control = e.Control
'
' NOT WORKING: Set location of the newly added control in a panel and at runtime
'
newControl.Location = New System.Drawing.Point(0, biasHeight)
'
' Store the y-cooridnate of the next control
'
biasHeight = biasHeight + newControl.Height
End Sub
End Class
【问题讨论】:
-
为什么要重新发明轮子?请改用
FlowLayoutPanel。 -
谢谢,是的,这绝对是正确的方法。虽然我仍然想了解为什么我的 UserControl 没有像我希望的那样工作。上面的示例只是我正在处理的控件的简化。