|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Modifiers of controls in UserControlsI have a problem with the visibility of private controls in my UserControl. My UserControl contains a private PictureBox and a private Label. When I put my UserControl on a form and get all controls of the form with: .... Dim myControls As ArrayList myControls = AllControls(Me) .... Public Function AllControls(ByVal frm As Form) As ArrayList Dim colControls As New ArrayList AddContainerControls(frm, colControls) Return colControls End Function Private Sub AddContainerControls(ByVal ctlContainer As Control, ByVal colControls As ArrayList) Dim ctl As Control For Each ctl In ctlContainer.Controls colControls.Add(ctl) AddContainerControls(ctl, colControls) Next End Sub ther is also the PictureBox and the Label from my UserControl and also my UserControl in myControls. Has anybody an idea how can get only my UserControl without the PictureBox and the Label? Thanks How about deleting this line?
For Each ctl In ctlContainer.Controls colControls.Add(ctl) ' AddContainerControls(ctl, colControls) Next Show quote :) I kid, I kid... Have you tried something like this?
private void AddContainerControls(Control ctl, ArrayList col) { Type t = ctl.GetType(); FieldInfo[] fields = t.GetFields(BindingFlags.Public | BindingFlags.Instance); for (int i = 0; i < fields.Length; i++) { Type c = typeof(Control); if (c.IsAssignableFrom(fields[i].FieldType)) { col.Add((Control)fields[i].GetValue(ctl)); AddContainerControls2((Control)fields[i].GetValue(ctl), col); } } } HTH, Eric Ya, it's only looking for Public controls. You can change the binding flags
to suit your needs. Also, maybe check the Parent is not your control type? private void AddContainerControls(Control ctl, ArrayList col) { Type t = ctl.GetType(); FieldInfo[] fields = t.GetFields(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance); for (int i = 0; i < fields.Length; i++) { Type c = typeof(Control); if (c.IsAssignableFrom(fields[i].FieldType)) { Control current = (Control)fields[i].GetValue(ctl); if (!(current.Parent is UserControl1)) { col.Add(current); AddContainerControls(current, col); } } } } -Eric |
|||||||||||||||||||||||