How to get a control's full location namespace?

AlexCode

Senior Contributor
Joined
Jul 7, 2003
Messages
931
Location
Portugal
Hi!
I need to get a string containing a specified control full location namespace...

Explaining it better, imagine this:
MyProject +-> MyForm1 +-> TabControl1 +-> TabPage1 +-> Button1

Giving the Button1 object as a parameter, I need to have a function that can retrieve me:

"MyProject.MyForm1.TabControl1.TabPage1.Button1"



My current solution looks like this:

Visual Basic:
    Shared Function GetParentTreeNamespace(ByVal ctrl As Control) As String
        If ctrl Is Nothing Then Return ""

        Dim pForm As Form = ctrl.FindForm
        Dim retval As String = ctrl.Name

        Dim cCtrl As Control = ctrl
        Do While Not cCtrl.Parent Is Nothing
            If cCtrl.Parent Is pForm Then
                retval = pForm.GetType.FullName & "." & retval
            Else
                retval = cCtrl.Parent.Name & "." & retval
            End If
            cCtrl = cCtrl.Parent
        Loop

        Return retval
    End Function

I was hoping that there were a solution without loops and stuff...

Thanks,
Alex :p
 
Considering the nature of the task, I can't see how this can be done without loops. I think that your method is as straightforward as possible.
 
Back
Top