Mi sono imbattuto ieri nella necessità di vedere se esiste un controllo in un container ( nel mio caso una page aspx) e ho provato a migrare la funzione in ambito win32..chissà se esiste un metodo migliore..
Public Function FindControl(ByVal objControl As Control, ByRef objContainer As Object) as boolean
Dim MyControlsArray As New ArrayList
Dim i As Integer
Try
For i = 0 To objContainer.controls.count - 1
Dim MyControl As Object = objContainer.Controls.Item(i)
If MyControl.Name = objControl.Name Then
Return True
End If
If MyControl.Controls.Count > 0 Then
MyControlsArray.Add(MyControl)
End If
Next
If MyControlsArray.Count > 0 Then
Dim MyArrayPosed As New ArrayList
MyArrayPosed = MyControlsArray
For i = 0 To MyArrayPosed.Count - 1
FindControl(objControl, MyArrayPosed(i))
Next
End If
Catch ex As SystemException
MsgBox(ex.Message)
Return False
End Try
End Function
La funzione simile in aspx era :
Public Shared Sub ShowBackOfficeLanguage(ByVal Language As BackOfficeLanguage, ByRef pContainer As Object)
Dim MyControl As System.Web.UI.Control
Dim MyControlName As String
Dim MyControlCollection As System.Web.UI.ControlCollection = CType(pContainer.Controls, System.Web.UI.ControlCollection)
For Each MyControl In MyControlCollection
MyControlName = MyControl.ID
'Esistono altri tre controlli con stesso nome e terminanti
'per _English e _Spanish e _French?
If Not IsNothing(pContainer.FindControl(MyControlName & "_English")) And _
Not IsNothing(pContainer.FindControl(MyControlName & "_Spanish")) And _
Not IsNothing(pContainer.FindControl(MyControlName & "_French")) Then
Dim MyControlEnglish As System.Web.UI.WebControls.WebControl = pContainer.FindControl(MyControlName & "_English")
Dim MyControlSpanish As System.Web.UI.WebControls.WebControl = pContainer.FindControl(MyControlName & "_Spanish")
Dim MyControlFrench As System.Web.UI.WebControls.WebControl = pContainer.FindControl(MyControlName & "_French")
Select Case Language
Case BackOfficeLanguage.Italian
MyControl.Visible = True
MyControlEnglish.Visible = False
MyControlSpanish.Visible = False
MyControlFrench.Visible = False
Case BackOfficeLanguage.English
MyControl.Visible = False
MyControlEnglish.Visible = True
MyControlSpanish.Visible = False
MyControlFrench.Visible = False
Case BackOfficeLanguage.Spanish
MyControl.Visible = False
MyControlEnglish.Visible = False
MyControlSpanish.Visible = True
MyControlFrench.Visible = False
Case BackOfficeLanguage.French
MyControl.Visible = False
MyControlEnglish.Visible = False
MyControlSpanish.Visible = False
MyControlFrench.Visible = True
End Select
End If
If MyControl.Controls.Count > 0 Then
ShowBackOfficeLanguage(Language, MyControl)
End If
Next
End Sub