循环遍历表单中的所有面板

Loop through all panels in form

我想遍历表单中的所有面板并设置可见 属性。 您能告诉我所有面板的存储位置吗?

Public Function ShowHide(PanelName As String)
    For Each sth As Panel In Form.Panels
        If sth.Name <> PanelName Then
            sth.visible = False
        Else
            sth.visible = True
        End If
    Next
End Function

试试这个:

Public Sub ShowHide(PanelName As String)
    For Each sth As Control In Me.controls
        If TypeOf sth Is Panel Then
            If sth.Name <> PanelName Then
                sth.Visible = False
            Else
                sth.Visible = True
            End If
        End If
    Next
End Sub

您可以使用 OfType() 方法只循环访问特定类型的控件:

Public Sub ShowHide(PanelName As String)
    For Each sth As Panel In Me.Controls.OfType(Of Panel)()
        If sth.Name = PanelName Then
            sth.Visible = True
        Else
            sth.Visible = False
        End If
    Next
End Sub

或者如果您想在一行中完成:

Public Sub ShowHide(PanelName As String)
    Me.Controls.OfType(Of Panel).ToList().ForEach(Sub(p) p.Visible = (p.Name = PanelName))
End Sub