繁体   English   中英

为每个控件处理容器内部的容器vb 2008

[英]Handle containers inside containers in for each control vb 2008

我创建了一个函数来翻译表单。 我可以遍历窗体中的每个控件以调用此函数,但是我遇到了一种情况,我无法处理。 在我的一种表单中,我在一个groupbox中有一个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

但是在某些情况下,我想在容器内分配控件。 我可以再循环一次

control.Controls(j)

是一个分组框,但是如果您知道我的意思,我想使此函数处理任何类型的“容器金字塔”。 也许我将有一个容器,其中也有一个容器,也有一个容器,等等...或者有没有可以用作组框的控件,但是它不算作容器,所以我可以通过以下方式看到它:

form.Controls

有什么建议么?

提前致谢。

您的代码未提供所需内容的原因是您没有对控件进行递归搜索。 请记住, Form.Controls仅包含父控件(而不是最终由父控件包含的子控件;就像您引用的由GroupBox包含的控件的情况一样)。 此外,我看到了各种不太正确的问题(您应该Option Strict On文件顶部写上Option Strict On ),这就是为什么此答案旨在为您提供某种更好的框架供您使用(您只需填写代码空白):

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像代码中那样遍历所有父控件(但是依赖于一组Subs (您错误地使用Function而不返回任何值,这是错误的),使结构更具适应性); 它还调用recursiveControls (它也为它分析的每个控件调用自身)以照顾可能存在的任何子控件。 我还包括actionsCurrentControl ,其中包含要为每个控件执行的所有操作(您必须用代码填充它)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM