Link to home
Start Free TrialLog in
Avatar of Ali Saad
Ali SaadFlag for Kuwait

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(ByVal 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("AccControls"))
DtviewLangE.Sort = "Control_id"


'********************************************
For ii = 0 To DtviewLangE.Count - 1
            CType(MyForm.Controls(DtviewLangE.Item(ii).Item("Control_id")), Control).Text = DtviewLangE.Item(ii).Item("Text_En")

Next
'********************************************
End sub

Avatar of David L. Hansen
David L. Hansen
Flag of United States of America image

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

    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

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hello, Idle_Mind,

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!  =)
Avatar of Ali Saad

ASKER

Thanks Idle_Mind it looks great solution. I will test it first then tell you the result.