首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在停靠模式下安排控件

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

Stack Overflow用户
提问于 2013-12-18 09:11:37
回答 1查看 223关注 0票数 0

我几乎花了两个小时在这些控制上做尝试和错误。我需要些帮助。

这就是我的目标..。为了能把他们三个都靠在右边,X-Y在那个进度栏下.以及X下面的轨迹杆..。(我最大限度地利用了我的形式,所以我需要停靠它)除非你有一些能自动适应的很酷的技巧。

X和Y是标签。显示我的地图坐标。

然后在它们下面是一个TrackBar在一个Panel里面。

尝试解决方案-结果:

  • 把它们靠岸-X面板Y/X Y面板
  • 把三个放在面板上,然后向右停靠-x,Y轨道,它消耗了太多的宽度。
  • 没有面板,停靠三个右轨道X
  • 船坞X Y顶部-出现在左上角,X低于Y。

我没有得到我想要的..。有什么想法吗?

EN

回答 1

Stack Overflow用户

发布于 2013-12-18 10:16:38

将所有控件添加到FlowLayoutPanel中。

将面板的流动方向设置为"RightToLeft“。

代码语言:javascript
复制
Me.FlowLayoutPanel1.FlowDirection = FlowDirection.RightToLeft 

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

替代

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

代码语言:javascript
复制
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
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20653818

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档