如何在停靠模式下安排控件

时间:2013-12-18 09:11:38

标签: .net vb.net winforms

我几乎花了2个小时对这些控件进行反复试验。我需要一些帮助。 enter image description here
是我的 GOAL ..能够将所有三个停靠在右侧,XY在该进度条下停靠..并且轨道栏位于X Y ..(我最大化我的形式所以我需要停靠它)除非你有一些可以自动适应的很酷的技巧..

X和Y 是标签..我显示地图坐标 然后它们在TrackBar ..

内是Panel

已尝试解决方案 - 结果:

  • 将它们对齐 - X面板Y / X Y面板
  • 将三个放到面板上并靠右 - X Y Track 消耗太多宽度
  • 没有面板,停靠三个右侧 - Track X Y
  • 停靠X Y顶部 - 出现在左上角,X低于Y

我没有达到我想要的......任何想法?

1 个答案:

答案 0 :(得分:2)

将所有控件添加到FlowLayoutPanel

将面板的流向设置为“RightToLeft”。

Me.FlowLayoutPanel1.FlowDirection = FlowDirection.RightToLeft 

在设计器中,选择面板中包含的所有控件,并将属性“FlowBreak”设置为“True”。

<强>替代

您也可以创建自己的流程面板。

Public Class MyFlowPanel
    Inherits Panel

    Protected Overrides Function CreateControlsInstance() As System.Windows.Forms.Control.ControlCollection
        Return New ControlCollection(Me)
    End Function

    Private Sub EnsureLayout(sender As Object, e As EventArgs)
        If (Not Me.isUpdatingLayout) Then
            Me.UpdateLayout()
        End If
    End Sub

    Private Sub NotifyControlIndexChanged(child As Control)
        'This will cause some selection issues in designer.
        'Implement a custom designer to fix this.
        Me.UpdateLayout()
    End Sub

    Protected Overrides Sub OnControlAdded(e As System.Windows.Forms.ControlEventArgs)
        AddHandler e.Control.SizeChanged, New EventHandler(AddressOf Me.EnsureLayout)
        AddHandler e.Control.LocationChanged, New EventHandler(AddressOf Me.EnsureLayout)
        AddHandler e.Control.Resize, New EventHandler(AddressOf Me.EnsureLayout)
        AddHandler e.Control.MarginChanged, New EventHandler(AddressOf Me.EnsureLayout)
        MyBase.OnControlAdded(e)
        Me.UpdateLayout()
    End Sub

    Protected Overrides Sub OnControlRemoved(e As System.Windows.Forms.ControlEventArgs)
        RemoveHandler e.Control.SizeChanged, New EventHandler(AddressOf Me.EnsureLayout)
        RemoveHandler e.Control.LocationChanged, New EventHandler(AddressOf Me.EnsureLayout)
        RemoveHandler e.Control.Resize, New EventHandler(AddressOf Me.EnsureLayout)
        RemoveHandler e.Control.MarginChanged, New EventHandler(AddressOf Me.EnsureLayout)
        MyBase.OnControlRemoved(e)
        Me.UpdateLayout()
    End Sub

    Protected Overrides Sub SetBoundsCore(x As Integer, y As Integer, width As Integer, height As Integer, specified As System.Windows.Forms.BoundsSpecified)
        MyBase.SetBoundsCore(x, y, width, height, specified)
        Me.UpdateLayout()
    End Sub

    Private Sub UpdateLayout()
        Me.isUpdatingLayout = True
        Dim top As Integer = Me.ClientRectangle.Top
        Dim right As Integer = Me.ClientRectangle.Right
        Dim c As Control
        For index = 0 To (Me.Controls.Count - 1)
            c = Me.Controls.Item(index)
            top += c.Margin.Top
            c.Location = New Point((right - (c.Width + c.Margin.Right)), top)
            top += (c.Height + c.Margin.Bottom)
        Next
        Me.isUpdatingLayout = False
    End Sub

    Private isUpdatingLayout As Boolean

    Public Shadows Class ControlCollection
        Inherits Panel.ControlCollection
        Public Sub New(owner As MyFlowPanel)
            MyBase.New(owner)
            Me.owner2 = owner
        End Sub
        Public Overrides Sub SetChildIndex(child As System.Windows.Forms.Control, newIndex As Integer)
            MyBase.SetChildIndex(child, newIndex)
            Me.owner2.NotifyControlIndexChanged(child)
        End Sub
        Private owner2 As MyFlowPanel
    End Class

End Class
相关问题