How To Access All Controls on a Form

Posted on 2008-11-06
Medium Priority
Last Modified: 2013-11-26
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

Question by:ali_alannah
LVL 15

Expert Comment

by:David L. Hansen
ID: 22896889
Use a for each loop where you act on each control in the passed in form's "Controls" collection.

LVL 12

Expert Comment

ID: 22897043
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

LVL 86

Accepted Solution

Mike Tomlinson earned 2000 total points
ID: 22897548
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
        ' ....do 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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

LVL 12

Expert Comment

ID: 22901818
Hello, Idle_Mind,

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

LVL 86

Expert Comment

by:Mike Tomlinson
ID: 22901892
There's always "new" things to be discovered as the versions march on without us...hard to keep up!  =)

Author Comment

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

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Loops Section Overview
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses
Course of the Month14 days, 16 hours left to enroll

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question