How to get the Child of a Container

merckrene

Member
Joined
Nov 3, 2009
Messages
9
Programming Experience
Beginner
I am creating a program but i dont know what code can get all the child of the container and control it anyone who knows how to do it?
 
Hi,

Every Container control has a Controls Collection which you can access to get any child controls that you need. As an example, lets say you wanted to change the BackColor of every Button Control on a Form you could use a For Loop to access the Forms Controls collection and then use the OfType(Of T) Method to get just the Buttons and then interact with each button as you need. i.e:-

For Each formButton As Button In Me.Controls.OfType(Of Button)()
  formButton.BackColor = Color.AliceBlue
Next


Hope that helps.

Cheers,

Ian
 
Hi,

Every Container control has a Controls Collection which you can access to get any child controls that you need. As an example, lets say you wanted to change the BackColor of every Button Control on a Form you could use a For Loop to access the Forms Controls collection and then use the OfType(Of T) Method to get just the Buttons and then interact with each button as you need. i.e:-

For Each formButton As Button In Me.Controls.OfType(Of Button)()
  formButton.BackColor = Color.AliceBlue
Next


Hope that helps.

Cheers,

Ian

it helps me a little but from this Just Some VB Code: How to find all child controls from a starting control or form helps me a lot with just few editing i solve my problem

VB.NET:
Dim Children As New List(Of System.Windows.Forms.Control)
        Dim oControl As System.Windows.Forms.Control
        For Each oControl In sender.parent.Controls
            Children.Add(oControl)
            If oControl.HasChildren Then
                Children.AddRange(oControl.FindForm.MdiChildren)
            End If
        Next
 
Back
Top