2

我创建了一个非常复杂的 Windows 窗体应用程序。我现在要做的是,如果单击不同的按钮以禁用某些面板,但将其下方的所有其他面板向上移动,使其看起来井井有条。我很难受。我想开发一个函数,它传入一个被禁用的面板以及它下面的所有其他面板的列表,并使用递归重新排列所有面板。此功能有效,但它没有考虑一个面板的高度是否大于另一个面板的高度,它会在两个面板之间留下额外的空间。任何帮助将非常感激。

Public Sub whatever(ByVal panel As Panel, ByVal list As List(Of Panel))
    Dim temppanel As Panel = New Panel()
    For Each item As Panel In list
        temppanel.Location = New Point(temppanel.Location.X, item.Location.Y)
        item.Location = New Point(item.Location.X, panel.Location.Y)
        list.Remove(item)
        whatever(temppanel, list)
        If list.Count = 0 Then
            Exit For
        End If
    Next
End Sub

图片

http://imageshack.us/photo/my-images/268/panelsz.png/

4

1 回答 1

2

FlowLayoutPanel 可能更适合这个,但这里有一个通用的方法来做我认为你正在寻找的东西:

Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) _
                          Handles Button1.Click
  ToggelPanel(Panel2)
End Sub

Private Sub ToggelPanel(ByVal whichPanel As Panel)
  Dim startPoint As New Point(10, 10)
  whichPanel.Visible = Not whichPanel.Visible

  For Each pnl As Panel In Me.Controls.OfType(Of Panel)(). _
                           Where(Function(x) x.Visible). _
                           OrderBy(Function(x) x.TabIndex)
    pnl.Location = startPoint
    startPoint.Y += pnl.Height + 4
  Next
End Sub

如果Me这些面板不在Me.


如果在 LINQ 不可用的情况下使用 .net 2.0,则该函数将如下所示:

Private Shared Function CompareTabIndex(ByVal p1 As Panel, ByVal p2 As Panel) _
                                        As Integer
  Return p1.TabIndex.CompareTo(p2.TabIndex)
End Function

Private Sub ToggelPanel(ByVal whichPanel As Panel)
  Dim startPoint As New Point(10, 10)
  whichPanel.Visible = Not whichPanel.Visible

  Dim panels As New List(Of Panel)
  For i As Integer = 0 To Me.Controls.Count - 1
    If TypeOf Me.Controls(i) Is Panel Then
      If Me.Controls(i).Visible Then
        panels.Add(Me.Controls(i))
      End If
    End If
  Next

  panels.Sort(New Comparison(Of Panel)(AddressOf CompareTabIndex))

  For Each pnl As Panel In panels
    pnl.Location = startPoint
    startPoint.Y += pnl.Height + pnl.Margin.Bottom
  Next
End Sub
于 2012-07-18T18:14:18.910 回答