Ali Saad
asked on
How To Access All Controls on a Form
I have a sub in a module ; that sub is called by all forms in my project ; what the sub do is to change the text in labels ,checkboxes, radiobottons ..etc. in each form also the sub change other properties for all controls in each form like (Left Property)
Module MyModule
Public Sub FlipMyFormEnglish (ByVal MyForm As Form)
----
----
End Sub
Public Sub TranslateMyFormEnglish (ByVal MyForm As Form)
----
----
End Sub
Public Sub FlipMyFormArabic (ByVal MyForm As Form)
----
----
End Sub
Public Sub TranslateMyFormArabic (ByVal MyForm As Form)
----
----
End Sub
End Module
And the code in each form is
If Me.RightToLeft = Windows.Forms.RightToLeft. Yes Then
FlipMyFormEnglish(Me)
TranslateMyFormEnglish(Me)
Else
FlipMyFormArabic(Me)
TranslateMyFormArabic(Me)
End If
I already saved all targeted controls in a table called AccControls has the following fields :-
Form_id
Control_id
Control_Type
Text_Original
Text_Ar
Text_En
I wrote the follwing code in sub called (TranslateMyForm) sub but it is NOT WORKING Because all controls are not on the form directly ; sometimes exists inside a panel and the panel inside a groupbox and the groupbox inside another groupbox :-
Public Sub TranslateMyFormEnglish(ByV al MyForm As Form)
'************************* *****
DstLangE = New DataSet
AdptLangE = New SqlDataAdapter("Select * from AccControls Where Form_id = '" & MyForm.Name & "' ", SqlCon)
AdptLangE.Fill(DstLangE, "AccControls")
DtviewLangE = New DataView(DstLangE.Tables(" AccControl s"))
DtviewLangE.Sort = "Control_id"
'************************* ********** *********
For ii = 0 To DtviewLangE.Count - 1
CType(MyForm.Controls(Dtvi ewLangE.It em(ii).Ite m("Control _id")), Control).Text = DtviewLangE.Item(ii).Item( "Text_En")
Next
'************************* ********** *********
End sub
Module MyModule
Public Sub FlipMyFormEnglish (ByVal MyForm As Form)
----
----
End Sub
Public Sub TranslateMyFormEnglish (ByVal MyForm As Form)
----
----
End Sub
Public Sub FlipMyFormArabic (ByVal MyForm As Form)
----
----
End Sub
Public Sub TranslateMyFormArabic (ByVal MyForm As Form)
----
----
End Sub
End Module
And the code in each form is
If Me.RightToLeft = Windows.Forms.RightToLeft.
FlipMyFormEnglish(Me)
TranslateMyFormEnglish(Me)
Else
FlipMyFormArabic(Me)
TranslateMyFormArabic(Me)
End If
I already saved all targeted controls in a table called AccControls has the following fields :-
Form_id
Control_id
Control_Type
Text_Original
Text_Ar
Text_En
I wrote the follwing code in sub called (TranslateMyForm) sub but it is NOT WORKING Because all controls are not on the form directly ; sometimes exists inside a panel and the panel inside a groupbox and the groupbox inside another groupbox :-
Public Sub TranslateMyFormEnglish(ByV
'*************************
DstLangE = New DataSet
AdptLangE = New SqlDataAdapter("Select * from AccControls Where Form_id = '" & MyForm.Name & "' ", SqlCon)
AdptLangE.Fill(DstLangE, "AccControls")
DtviewLangE = New DataView(DstLangE.Tables("
DtviewLangE.Sort = "Control_id"
'*************************
For ii = 0 To DtviewLangE.Count - 1
CType(MyForm.Controls(Dtvi
Next
'*************************
End sub
Use a for each loop where you act on each control in the passed in form's "Controls" collection.
Hello, ali_alannah,
I suggest that you replace MyForm.Controls in your loop with a recursive search routine such as in the attached snippet. This function returns a Control with the specified Name from the "Container" (in this case, your Form). The search is recursive and not case sensitive.
Please be aware though, that this technique requires that only one control on the form has the name being sought. If this is not the case, you may wish to include some form of lineage in your data table to ensure you find the intended control.
Cheers,
Randy
I suggest that you replace MyForm.Controls in your loop with a recursive search routine such as in the attached snippet. This function returns a Control with the specified Name from the "Container" (in this case, your Form). The search is recursive and not case sensitive.
Please be aware though, that this technique requires that only one control on the form has the name being sought. If this is not the case, you may wish to include some form of lineage in your data table to ensure you find the intended control.
Cheers,
Randy
Function GetControlByName(ByVal HostContainer As Control, _
ByVal ControlName As String) As Control
Dim ControlFound As Control = HostContainer.Controls(ControlName)
If (ControlFound IsNot Nothing) Then Return ControlFound
For Each ContainedControl As Control In HostContainer.Controls
Dim NestedControl As Control
NestedControl = GetControlByName(ContainedControl, ControlName)
If (Not NestedControl Is Nothing) Then Return NestedControl
Next ContainedControl
Return Nothing ' No match was found.
End Function
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Hello, Idle_Mind,
Hey, I didn't know about that. Thanks! That's definitely a better way to go.
Cheers,
Randy
Hey, I didn't know about that. Thanks! That's definitely a better way to go.
Cheers,
Randy
There's always "new" things to be discovered as the versions march on without us...hard to keep up! =)
ASKER
Thanks Idle_Mind it looks great solution. I will test it first then tell you the result.