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
End If

I already saved all targeted controls in a table called AccControls has the following fields :-


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")

End sub

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

David L. HansenCEOCommented:
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.


    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

Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
If you have VB.Net 2005 (or above) then you can SEARCH for the control without using a recursive algorithm:

    Dim ctlName As String = "someControlName"
    Dim matches() As Control
    matches = Me.Controls.Find(ctlName, True)
    If matches.Length > 0 Then
        ' something with matches...

        ' one example of what can be done:
        If TypeOf matches(0) Is TextBox Then
            Dim tb As TextBox = DirectCast(matches(0), TextBox)
            tb.Text = "Hello World!"
        End If
        MessageBox.Show(ctlName & " not found")
    End If

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
PMI ACP® Project Management

Prepare for the PMI Agile Certified Practitioner (PMI-ACP)® exam, which formally recognizes your knowledge of agile principles and your skill with agile techniques.

Hello, Idle_Mind,

Hey, I didn't know about that.  Thanks!  That's definitely a better way to go.

Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
There's always "new" things to be discovered as the versions march on without us...hard to keep up!  =)
ali_alannahAuthor Commented:
Thanks Idle_Mind it looks great solution. I will test it first then tell you the result.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.