Joe88
Joe88

Reputation: 441

Handle containers inside containers in for each control vb 2008

I have created a function to translate my forms. I can loop through every control in a form to call this function, but I have made a situation, I cannot handle. In one of my forms, I have groupbox in a groupbox. This source works if I only have one groupbox.

    Public Function translate_form(ByVal form As Form)
    Dim control As Object
    Dim controlname As String

    form.Text = Get_Control_Name(form.Name, "Form")
    Try
        For i = 0 To form.Controls.Count - 1
            control = form.Controls(i)
            If TypeOf (control) Is MenuStrip Then
                For j = 0 To control.items.count - 1
                    control.items(j).text = Get_Control_Name(form.Name, "MenuItem" & j)
                Next
            Else
                controlname = Get_Control_Name(form.Name, control.Name)
                control.Text = IIf(controlname Is Nothing, control.Text, controlname)
                If TypeOf (control) Is GroupBox Then
                    For j = 0 To control.Controls.Count - 1
                        controlname = Get_Control_Name(form.Name, control.Controls(j).Name)
                        control.Controls(j).Text = IIf(controlname Is Nothing, control.Controls(j).Text, controlname)
                        If TypeOf (control.Controls(j)) Is Button Then
                            control.Controls(j).AutoSize = True
                        End If
                    Next
                End If
                If TypeOf (control) Is Button And UCase(control.Text) <> "X" Then
                    control.AutoSize = True
                End If
            End If
        Next
    Catch ex As Exception
        MsgBox(ex.Message, MsgBoxStyle.Critical)
    End Try
End Function

But in some cases I want to sperate controls inside a container. I could have one more loop if the

control.Controls(j)

is a groupbox but I want to make this function to handle any kind of "container pyramid", if you know what I mean. Maybe I will have a container which has one also and that one too etc... Or is there any control I can use as a groupbox but it doesn't count as a contaier, so I can see it with:

form.Controls

Any suggestions?

Thanks in advance.

Upvotes: 0

Views: 218

Answers (1)

user2480047
user2480047

Reputation:

The reason why your code does not deliver what you want is that you are not performing a recursive search of controls. Bear in mind that Form.Controls contains only the parent controls (not the children controls eventually contained by the parents; like the situation you refer of controls contained by a GroupBox). Additionally, I see various not-too-right issues (you should writeOption Strict On on the top of your file) and that's why this answer intends to provide you with a somehow better framework to work with (you have just to fill in the blanks with your code):

Public Sub translate_form2(ByVal form As Form)

    Try

        For Each ctrl As Control In form.Controls
            actionsCurrentControl(ctrl)
            recursiveControls(ctrl)
        Next

    Catch ex As Exception
    End Try

End Sub

'Accounting for all the child controls (if any)
Public Sub recursiveControls(parentControl As Control)

    If (parentControl.HasChildren) Then

        For Each ctrl As Control In parentControl.Controls
            actionsCurrentControl(ctrl)
            recursiveControls(ctrl)
        Next

    End If

End Sub

Public Sub actionsCurrentControl(curControl As Control)

    If TypeOf curControl Is MenuStrip Then

    Else
        If TypeOf (curControl) Is GroupBox Then

        End If
        If TypeOf (curControl) Is Button And UCase(curControl.Text) <> "X" Then

        End If
    End If

End Sub

translate_form2 iterates through all the parent controls as in your code (but by relying on a set of Subs (you are wrongly using a Function without returning any value, what is wrong), making the structure more adaptable); it also calls recursiveControls (which also calls itself for each control it analyses) to take care of any child control which might be present. I am also including actionsCurrentControl which contains all the actions to perform for each control (you have to populate it with your code).

Upvotes: 1

Related Questions