隐藏一个VB.Net后重新安排面板

时间:2012-07-18 17:21:51

标签: vb.net winforms

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

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/

1 个答案:

答案 0 :(得分: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