Checking if a control exists within a container control?

I have a routine that changes various properties of controls. Within this routine, I need to check each control that is passed into it whether or not they are contained within a control. I'm sure this can be done but I've never done it before and don't know how to check for it.

Thanks!
BlakeMcKennaAsked:
Who is Participating?
 
Mike TomlinsonConnect With a Mentor Middle School Assistant TeacherCommented:
?...Control.Parent() returns type Control.  Just access it!

Assuming it is not Nothing:
ctl.BackColor = ctl.Parent.BackColor

Open in new window

0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
What issue(s) are you having with controls that are not contained by something?...we need more details to even begin to help with this one as it's too vague.
0
 
BlakeMcKennaAuthor Commented:
I'm trying to set the background color of a control that resides within a container control to the container control's background color.
0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Well, that would be difficult to do if said control doesn't have a container!  =)

You can check the Parent() property to see if it has a value.  It it is Nothing, then no container is set:
        If Not IsNothing(ctl.Parent) Then
            ' ... control has a container ...
        Else
            ' ... control does NOT have a container ...
        End If

Open in new window

0
 
BlakeMcKennaAuthor Commented:
Ok,

With the above code, how can I extract the Parent's BackColor or DefaultBackColor value?
0
 
BlakeMcKennaAuthor Commented:
Thanks...that worked!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.