Function GetControlByName(ByRef oForm As System.Windows.Forms.Form, ByVal sControlName As String) As System.Windows.Forms.Control
Dim oCtrl As System.Windows.Forms.Control
oCtrl = oForm.Controls(sControlName)
If oCtrl Is Nothing Then
For Each oCtrl In oForm.Controls.Find(sControlName, True)
If oCtrl.Name.Equals(sControlName) Then Return oCtrl
Next
Else
Return oCtrl
End If
Return Nothing
End Function
Remarque :
TextBox1, une zone texte sur la form
TextBox2, une zone texte dans un groupbox
DirectCast(GetControlByName(Me, "TextBox1"), System.Windows.Forms.TextBox).Text = "TextBox1"
DirectCast(GetControlByName(Me, "TextBox2"), System.Windows.Forms.TextBox).Text = "TextBox2"