在 Vb.Net 中查找控件的最终父级

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21011971/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 16:23:29  来源:igfitidea点击:

Finding Ultimate Parent of a Control in Vb.Net

vb.netwinformsuser-controlssystem.reflection

提问by user3169552

I am new to Visual Basic.NET ,I need to write down a piece of code to find the Top-Most Parent of a (User Control/Control) in Windows Form.

我是 Visual Basic.NET 的新手,我需要写下一段代码才能在 Windows 窗体中找到(用户控件/控件)的最高父级。

I have hundreds of controls on to Windows Form, some are User Controls and some built-in Windows controls

我有数百个 Windows 窗体控件,一些是用户控件和一些内置的 Windows 控件

The code I have tested is adding multiple IF conditions but when controls are nested more then 2 levels then its hard to add IF conditions.

我测试的代码添加了多个 IF 条件,但是当控件嵌套超过 2 个级别时,很难添加 IF 条件。

Like: Form --Panel ----Panel ------GroupBox --------TextBox

如: 表单 --Panel ----Panel ------GroupBox --------TextBox

'Here is simple code
'A TextBox inside Panel control
Dim parent_control As Control = TryCast(txtbox, Control) 'casting that Control in generic Control
if parent_control.Parent Is Nothing Then
   Return 
Else
   Return parent_control.Parent.Parent
End If

I would be very thankful if somebody guide me in this regard.

如果有人在这方面指导我,我将非常感激。

回答by DareDevil

Here is done through recursion:

这里是通过递归完成的:

#Region "Get Ultimate Parent"
Private Function GetParentForm(ByVal parent As Control) As Control
    Dim parent_control As Control = TryCast(parent, Control)
    '------------------------------------------------------------------------
    'Specific to a control means if you want to find only for certain control
    If TypeOf parent_control Is myControl Then   'myControl is of UserControl
        Return parent_control
    End If
    '------------------------------------------------------------------------
    If parent_control.Parent Is Nothing Then
        Return parent_control
    End If
    If parent IsNot Nothing Then
        Return GetParentForm(parent.Parent)
    End If
    Return Nothing
End Function
#End Region

It worked for me perfectly.

它非常适合我。

回答by nostramaroine

**You can use just do

**您可以使用just do

Dim Form As System.Windows.Forms.Form
Form = Combobox.FindForm()  

'to find directy the parent form of any controle without any For until**

'直接查找任何控件的父窗体,没有任何 For 直到* *

回答by Sam Axe

no need for recursion here.

这里不需要递归。

Private Function UltimateParent(ByVal control as Control) As Control

  Do
    If Nothing Is control.Parent
      Return control
    Else
      control = control.Parent
    End If
  Loop

End Function

回答by Rex

the ultimate would be the form, but you are really looking for a method to trace through, you could use either recursion or while loop:

最终将是形式,但您确实在寻找一种方法来跟踪,您可以使用递归或 while 循环:

Public Function FindTopMostParent(ctrl As Control) As Control
    If ctrl.Parent Is Nothing Then
        Return ctrl '// or nothing?
    End If

    Return FindTopMostParent(ctrl.Parent)
End Function

Public Function FindTopMostParent_v2(ctrl As Control) As Control
    Dim output As Control = ctrl

    While output.Parent IsNot Nothing
        output = output.Parent
    End While

    Return output
End Function

回答by Zasqa

Simplest

最简单

Public Function GetUltimateParent(ByVal ofThisControl As Control) As Control
    If ofThisControl Is Nothing return Nothing 'Error Check

    Dim ultimateParent As Control = ofThisControl
    While Not ultimateParent.Parent Is Nothing
        ultimateParent = ultimateParent.Parent
    End While

    Return ultimateParent
 End Function