• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 464
  • Last Modified:

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

0
ali_alannah
Asked:
ali_alannah
1 Solution
 
David L. HansenProgrammer AnalystCommented:
Use a for each loop where you act on each control in the passed in form's "Controls" collection.

0
 
omegaomegaDeveloperCommented:
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

0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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
    Else
        MessageBox.Show(ctlName & " not found")
    End If

0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
omegaomegaDeveloperCommented:
Hello, Idle_Mind,

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

Cheers,
Randy
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
There's always "new" things to be discovered as the versions march on without us...hard to keep up!  =)
0
 
ali_alannahAuthor Commented:
Thanks Idle_Mind it looks great solution. I will test it first then tell you the result.
 
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now