【问题标题】:Finding Ultimate Parent of a Control in Vb.Net在 Vb.Net 中查找控件的最终父级
【发布时间】:2014-01-09 05:05:30
【问题描述】:

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

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

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

喜欢: 表格 - 控制板 - - 控制板 ------分组框 --------文本框

'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

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

【问题讨论】:

  • 当你说你被问到 ...我想知道是否真的应该给你一个答案。所以,提示:尝试递归?
  • 先生,其实是一个函数编写

标签: vb.net winforms user-controls system.reflection


【解决方案1】:

**你可以用just do

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

'直接查找任何控件的父窗体,不带任何 For until**

【讨论】:

    【解决方案2】:

    这里是通过递归完成的:

    #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
    

    它非常适合我。

    【讨论】:

      【解决方案3】:

      这里不需要递归。

      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
      

      【讨论】:

        【解决方案4】:

        最终将是表单,但您确实在寻找一种跟踪方法,您可以使用递归或 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
        

        【讨论】:

        • Rex: 最重要的是Form很明显,但是在UserControl的情况下没有Form,所以对于用户控件来说它就是类名。
        【解决方案5】:

        最简单的

        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
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2023-03-10
          • 2014-12-13
          相关资源
          最近更新 更多