Accès à un contrôle qui se trouve dans 1 à plusieurs containers
Le
Bonjour,
Dans un module public où je passe en paramètre la Form appelante, je connais
le nom du contrôle que je cherche (ex : TextBox_Nom).
Ce contrôle se trouve dans 1 à plusieurs contrôle containers
(TableLayoutPanel, TabControl et GroupBox) mais le chemin pour accéder à
chaque contrôle cherché n'est pas toujours le même.
Ex :
Dim tb_nom As TextBox =
CType(Form_Appelante..Controls("TableLayoutPanel_Racine").Controls("GroupBox_Niveau_1").Controls("TextBox_Nom"),
TextBox )
Dim cb_niveau As ComboBox =
CType(Form_Appelante.Controls("TableLayoutPanel_Racine").Controls("TabControl_Description").Controls("TableLayoutPanel_Page_1").Controls("GroupBox_Niveau_2").Controls("ComboBox_Niveau"),
ComboBox)
Cette méthode d'accès fonctionne, mais je trouve que c'est long pour chaque
contrôle.
Je préfèrerais si c'est possible une fonction récursive qui trouverait le
contrôle cherché.
J'ai essayé ceci, mais sans succès :
Dim test_cb_niveau As ComboBox = cherche_cb(Form_Appelante,
"ComboBox_Niveau")
Function cherche_cb(ByVal c As ContainerControl, ByVal cb As String) As
ComboBox
For Each ctrl As Control In c.Controls
If ctrl.Name = cb Then
Return CType(ctrl, ComboBox)
Else
If ctrl.GetType.ToString = "System.Windows.Forms.TableLayoutPanel" OrElse _
ctrl.GetType.ToString = "System.Windows.Forms.TabControl" OrElse _
ctrl.GetType.ToString = "System.Windows.Forms.GroupBox" Then
cherche_cb = cherche_cb(CType(ctrl, ContainerControl), cb)
If Not (cherche_cb Is Nothing) Then
Return cherche_cb
End If
End If
End If
Next
Return Nothing
End Function
Avec cette fonction, une exception se produit :
L'exception System.InvalidCastException n'a pas été gérée
Message="Impossible d'effectuer un cast d'un objet de type
'System.Windows.Forms.TableLayoutPanel' en type
'System.Windows.Forms.ContainerControl'."
Existe-t-il une méthode qui me permettrait de trouver la solution à mon
problème ? (sinon, je garde l'ancienne méthode)
Merci d'avance,
Stéphane
Dans un module public où je passe en paramètre la Form appelante, je connais
le nom du contrôle que je cherche (ex : TextBox_Nom).
Ce contrôle se trouve dans 1 à plusieurs contrôle containers
(TableLayoutPanel, TabControl et GroupBox) mais le chemin pour accéder à
chaque contrôle cherché n'est pas toujours le même.
Ex :
Dim tb_nom As TextBox =
CType(Form_Appelante..Controls("TableLayoutPanel_Racine").Controls("GroupBox_Niveau_1").Controls("TextBox_Nom"),
TextBox )
Dim cb_niveau As ComboBox =
CType(Form_Appelante.Controls("TableLayoutPanel_Racine").Controls("TabControl_Description").Controls("TableLayoutPanel_Page_1").Controls("GroupBox_Niveau_2").Controls("ComboBox_Niveau"),
ComboBox)
Cette méthode d'accès fonctionne, mais je trouve que c'est long pour chaque
contrôle.
Je préfèrerais si c'est possible une fonction récursive qui trouverait le
contrôle cherché.
J'ai essayé ceci, mais sans succès :
Dim test_cb_niveau As ComboBox = cherche_cb(Form_Appelante,
"ComboBox_Niveau")
Function cherche_cb(ByVal c As ContainerControl, ByVal cb As String) As
ComboBox
For Each ctrl As Control In c.Controls
If ctrl.Name = cb Then
Return CType(ctrl, ComboBox)
Else
If ctrl.GetType.ToString = "System.Windows.Forms.TableLayoutPanel" OrElse _
ctrl.GetType.ToString = "System.Windows.Forms.TabControl" OrElse _
ctrl.GetType.ToString = "System.Windows.Forms.GroupBox" Then
cherche_cb = cherche_cb(CType(ctrl, ContainerControl), cb)
If Not (cherche_cb Is Nothing) Then
Return cherche_cb
End If
End If
End If
Next
Return Nothing
End Function
Avec cette fonction, une exception se produit :
L'exception System.InvalidCastException n'a pas été gérée
Message="Impossible d'effectuer un cast d'un objet de type
'System.Windows.Forms.TableLayoutPanel' en type
'System.Windows.Forms.ContainerControl'."
Existe-t-il une méthode qui me permettrait de trouver la solution à mon
problème ? (sinon, je garde l'ancienne méthode)
Merci d'avance,
Stéphane

Poser une question


J'ai ce qu'il te faut ;).. en fait cela existe déjà dans le framework.
La collection Controls de Control contient une méthode Find() qui permet
de rechercher un contrôle même parmi les collections Controls imbriquées.
Par exemple dans ton cas tu peux faire un code du style :
Form_Appelante.Controls.Find(controlName, True).FirstOrDefault()
Le FirstOrDefault viens de Linq et te permet de récupérer le premier
control qui correspond au nom si il en existe un.
--
Jérémy JEANSON
MCP
http://www.jjeanson.fr
Merci beaucoup Jérémy !!!
Stéphane
"Jérémy Jeanson"