简体   繁体   English

获取子面板的父级名称

[英]Get the name of the Parent of a child Panel

I have a Panel object, which was dynamically created within another Panel object that was also dynamically created.我有一个面板 object,它是在另一个也是动态创建的面板 object 中动态创建的。
How can I get the name of the parent Panel from the child Panel?.如何从子面板中获取父面板的名称?

I only found information related to the Form object.我只找到了与表格 object 相关的信息。

A couple of methods that may help in finding the child's Parents.有几种方法可能有助于找到孩子的父母。

The direct parent of a Control is returned by the Control.Parent property: Control.Parent属性返回 Control 的直接父级:

Dim parentName = [SomeControl].Parent.Name

The Form container is returned by the FindForm() method or the Control.TopLevelControl property: Form 容器由FindForm()方法或Control.TopLevelControl属性返回:

Dim myForm1 = [SomeControl].FindForm()
Dim myForm2 = [SomeControl].TopLevelControl
Dim myFormName1 = myForm1.Name
Dim myFormName2 = myForm2.Name

You may also use GetContainerControl() , this returns the outermost IContainerControl .您也可以使用GetContainerControl() ,这将返回最外面的IContainerControl

A UserControl can use the ParentForm property (but it's the same as FindForm() ) UserControl 可以使用ParentForm属性(但它与FindForm()相同)

To find the outer container that is not a Form:要查找不是表单的外部容器:

Private Function FindOuterContainer(ctrl As Control) As Control
    If ctrl Is Nothing Then Return Nothing
    While Not (TypeOf ctrl.Parent Is Form)
        ctrl = FindOuterContainer(ctrl.Parent)
    End While
    Return ctrl
End Function

Dim outerContainer = FindOuterContainer([SomeControl])
Dim outerContainerName = outerContainer.Name

To find the outer ancestor of a specific type (eg, you have a Panel inside a Panel inside a TabPage of a TabControl and you want to know what TabPage that is):要查找特定类型的外部祖先(例如,您在 TabControl 的 TabPage 内的 Panel 中有一个 Panel,并且您想知道 TabPage 是什么):

Private Function FindOuterContainerOfType(Of T)(ctrl As Control) As Control
    If ctrl Is Nothing Then Return Nothing
    While Not ((TypeOf ctrl.Parent Is Form) OrElse (TypeOf ctrl Is T))
        ctrl = FindOuterContainerOfType(Of T)(ctrl.Parent)
    End While
    Return ctrl
End Function

Dim parentTabPage = FindOuterContainerOfType(Of TabPage)([SomeControl])
Console.WriteLine(parentTabPage.Name)

To find the outermost Parent of a specific type:要查找特定类型的最外层父级:
(eg, you have a Panel inside a Panel inside a TabPage of a TabControl which is inside a Panel and you want to get this last Panel) (例如,您在一个面板内的一个面板内有一个面板,该面板位于面板内的 TabControl 的 TabPage 内,并且您想要获得最后一个面板)

Private Function FindOuterMostContainerOfType(Of T)(ctrl As Control) As Control
    If ctrl Is Nothing Then Return Nothing
    Dim outerParent As Control = Nothing
    While Not (TypeOf ctrl.Parent Is Form)
        If TypeOf ctrl.Parent Is T Then outerParent = ctrl.Parent
        ctrl = ctrl.Parent
    End While
    Return If(TypeOf outerParent Is T, outerParent, Nothing)
End Function


Dim outermostParentPanel = 
    TryCast(FindOuterMostContainerOfType(Of Panel)([SomeControl]), Panel)
Dim outermostParentPanelName = outermostParentPanel?.Name

[SomeControl] is of course the instance of the child Control that wants to find its Parents. [SomeControl]当然是想要找到它的父控件的子控件的实例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM