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"
Langage :
VB6
Date ajout :
10/03/2010
Posté par
PCPT
[
Liste]
Function GetControlByName(ByRef oForm As Form, ByVal sControlName As String) As Control
Dim oCtrl As Object
Dim Index As Integer
Dim iGpePos As Integer
' groupe de controles?
iGpePos = VBA.InStr(1, sControlName, "(")
If iGpePos Then
Index = VBA.Val(VBA.Mid(sControlName, iGpePos + 1, VBA.Len(sControlName) - 1 - iGpePos))
sControlName = VBA.Left$(sControlName, iGpePos - 1)
Else
Index = -1
End If
' boucle
For Each oCtrl In oForm
If oCtrl.Name = sControlName Then
If Index = -1 Then
Set GetControlByName = oCtrl
Exit For
Else
' groupe
If oCtrl.Index = Index Then
Set GetControlByName = oCtrl
Exit For
End If
End If
End If
Next oCtrl
Set oCtrl = Nothing
End Function
Remarque :
Text1, une zone texte sur la form
Text2, une zone texte dans une frame
Text3, une zone texte INDEXEE (à zéro) dans une frame
GetControlByName(Me, "Text1").Text = "TextBox1"
GetControlByName(Me, "Text2").Text = "TextBox2"
GetControlByName(Me, "Text3(0)").Text = "1ère TextBox3"