1 pièce(s) jointe(s)
Boucler (For/For Each) sur toutes les TextBox (Control) d'un Formulaire (Form)
Bonjour à tous,
Je suis tout nouveau au niveau de la programmation en Visual Basic (VB 11.0 & .NET 4.5).
Je suis en cours d'apprentissage, et juste avant, il y a quelques temps, je programmais un peu en VBA.
Ici, en VB, j'aurai aimé connaitre la syntaxe la plus simple pour pouvoir boucler sur toutes les textBox d'un formulaire (Form).
Pour l'instant, voici mon code:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
Private Sub Button2_Click(sender As Object, e As System.EventArgs) Handles Button2.Click
For Each element As Forms.Control In Me.Controls
If TypeOf element Is Forms.GroupBox Then
For Each elementChild1 As Forms.Control In element.Controls
If TypeOf elementChild1 Is Forms.GroupBox Then
For Each elementChild2 As Forms.Control In elementChild1.Controls
If TypeOf elementChild2 Is Forms.TextBox Then
DirectCast(elementChild2, Forms.TextBox).Text = Nothing
End If
Next
End If
Next
End If
Next
End Sub |
Comme vous le voyez, il est relativement complexe pour si peu, mais c'est pour l'instant la solution que j'ai retenu.
Ici, les TextBox sont contenu dans des GroupBox, mais imaginez que je ne sache pas vraiment où soient les TextBox et qu'il y en ai dispatché un peu partout dans le formulaire et dans n'importe quel type de conteneurs...
En bref, y a t-il une manière plus simple et plus générale pour directement accéder à la totalité des TextBox d'un Form ? Si oui, laquelle justement.
Merci d'avance, A+
P.S.:
Options:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
</PropertyGroup>
<PropertyGroup>
<OptionExplicit>On</OptionExplicit>
</PropertyGroup>
<PropertyGroup>
<OptionCompare>Binary</OptionCompare>
</PropertyGroup>
<PropertyGroup>
<OptionStrict>On</OptionStrict>
</PropertyGroup>
<PropertyGroup>
<OptionInfer>Off</OptionInfer>
</PropertyGroup>
<PropertyGroup> |
Espaces de nom:
Code:
1 2 3 4 5 6 7 8 9
|
<ItemGroup>
<Import Include="Console = System.Console" />
<Import Include="Forms = System.Windows.Forms" />
<Import Include="Tasks = System.Threading.Tasks" />
<Import Include="Thread = System.Threading.Thread" />
<Import Include="Timer = System.Diagnostics.Stopwatch" />
<Import Include="Vb = Microsoft.VisualBasic" />
</ItemGroup> |
Images (pour voir les GroupBox) :
Pièce jointe 149696
--------------------------------------------------------------------------------
Tant qu'on y ai, j'ai justement une autre question, ça tombe bien :mrgreen:
Dans le même projet test (donc même Option et Imports), j'ai traduit le code suivant en VB : http://blogs.msdn.com/b/shawnhar/arc...c-dynamic.aspx
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
|
Namespace SampleHeritage
Class Animal
End Class
Class Cat : Inherits Animal
End Class
Class Dog : Inherits Animal
End Class
Class Mouse : Inherits Animal
End Class
Module M1
'We can create several overloads of the same method, specialized according to different combinations of their parameter types:
Sub ReactSpecialization([me] As Animal, other As Animal)
Console.WriteLine("{0} is not interested in {1}.", [me], other)
End Sub
Sub ReactSpecialization([me] As Cat, other As Dog)
Console.WriteLine("Cat runs away from dog.")
End Sub
Sub ReactSpecialization([me] As Cat, other As Mouse)
Console.WriteLine("Cat chases mouse.")
End Sub
Sub ReactSpecialization([me] As Dog, other As Cat)
Console.WriteLine("Dog chases cat.")
End Sub
'And now the magic part:
Sub React([me] As Animal, other As Animal)
ReactSpecialization([me], other)
End Sub
'This works because of the "as dynamic" cast, which tells the C# compiler,
'rather than just calling ReactSpecialization(Animal, Animal),
'to dynamically examine the type of each parameter and make a runtime choice
'about which method overload to invoke.
'To prove it really works:
Sub Test()
Dim cat As Animal = New Cat()
Dim dog As Animal = New Dog()
Dim mouse As Animal = New Mouse()
React(cat, dog)
React(cat, mouse)
React(dog, cat)
React(dog, mouse)
End Sub
'Output:
' Cat runs away from dog.
' Cat chases mouse.
' Dog chases cat.
' Dog is not interested in Mouse.
End Module
End Namespace |
En langage C# et en utilisant un type Dynamic (as dynamic), la sortie du Debug.Print est:
Code:
1 2 3 4 5 6 7
|
'Output:
' Cat runs away from dog.
' Cat chases mouse.
' Dog chases cat.
' Dog is not interested in Mouse. |
Si on essaye ce code en VB, la sortie sera:
Code:
1 2 3 4 5
|
' Cat is not interested in Dog.
' Cat is not interested in Mouse.
' Dog is not interested in Cat.
' Dog is not interested in Mouse. |
parce que j'ai remplacé le Type dynamic (C#) en type Animal.
D'après vous, comment obtenir le même résultat qu'en C#.
On voit qu'en exécutant le code, le "Sous-type" est bien respectivement Cat, Dog et Mouse lors de l'initialisation dans la procédure Sub Test().