VB.Net:遍历所有表单项,包括CommonDialogs

VB.Net:遍历所有表单项,包括CommonDialogs,vb.net,controls,translation,winforms,common-dialog,Vb.net,Controls,Translation,Winforms,Common Dialog,我正在翻译我的VB.Net应用程序,我需要遍历表单上的所有控件。使用递归函数,例如 Public Sub TranslateControl(ByVal Ctrl As Control) For Each ChildCtrl As Control In Ctrl.Controls ChildCtrl.Text = Translate(ChildCtrl.Text) If TypeOf ChildCtrl Is Label Then

我正在翻译我的VB.Net应用程序,我需要遍历表单上的所有控件。使用递归函数,例如

Public Sub TranslateControl(ByVal Ctrl As Control)
    For Each ChildCtrl As Control In Ctrl.Controls
        ChildCtrl.Text = Translate(ChildCtrl.Text)

        If TypeOf ChildCtrl Is Label Then
            CType(ChildCtrl, Label).Tag = Translate(CType(ChildCtrl, Label).Tag)
        End If

        TranslateControl(ChildCtrl)
    Next
End Sub
工作非常好,但它不包括
CommonDialog
对象,例如
FolderBrowser
对象。如何访问这些对象?我试过这个

    For Each ChildDialog As CommonDialog In Ctrl.Controls
        ChildDialog.Tag = Translate(ChildDialog.Tag)
    Next
但显然存在继承问题,因为
CommonDialog
对象不是
控件

是否有一种方法可以让我循环查看表单上显示的所有项目

非常感谢


不,它们是组件,不是控件。他们的代码实际上存在于shell中,由Microsoft使用非托管C/C++编写。唯一可以管理它们的是一个小包装器,它可以进行必要的API调用来显示它们并返回结果。例如OpenFileDialog

您将遇到的第一个问题是在显示这样一个对话框时运行代码。它是一个对话框,在调用ShowDialog()之后,控件不会返回到程序,直到用户将其取消。这是可能的,有相当多的欺骗。请检查我的代码以了解方法。如前所述,该代码适用于任何shell对话框以及MessageBox


这将获取对话框的窗口句柄。接下来,您必须迭代对话框的子窗口。您可以通过EnumChildWindows API调用实现这一点。这将为您提供每个子项的窗口句柄,然后您可以使用SendMessage()对该子项执行某些操作。不管是什么,您在问题中没有具体说明。

不,它们是组件,不是控件。他们的代码实际上存在于shell中,由Microsoft使用非托管C/C++编写。唯一可以管理它们的是一个小包装器,它可以进行必要的API调用来显示它们并返回结果。例如OpenFileDialog

 Friend Sub resetFormControls(zForm As Form)
您将遇到的第一个问题是在显示这样一个对话框时运行代码。它是一个对话框,在调用ShowDialog()之后,控件不会返回到程序,直到用户将其取消。这是可能的,有相当多的欺骗。请检查我的代码以了解方法。如前所述,该代码适用于任何shell对话框以及MessageBox

这将获取对话框的窗口句柄。接下来,您必须迭代对话框的子窗口。您可以通过EnumChildWindows API调用实现这一点。这将为您提供每个子项的窗口句柄,然后您可以使用SendMessage()对该子项执行某些操作。不管是什么,你在问题中没有具体说明

 Friend Sub resetFormControls(zForm As Form)
尝试使用子例程将所有控件重置回未使用状态:空白文本框、未选中复选框和单选按钮等

        For Each zCntl As Control In zForm.Controls
            If zCntl.HasChildren Then
                For Each zChildCntl As Control In zCntl.Controls
                    If zChildCntl.GetType Is GetType(CheckBox) Then
                        CType(zChildCntl, CheckBox).Checked = False
                    End If

                    If zChildCntl.GetType Is GetType(TextBox) Then CType(zChildCntl, TextBox).Text = ""
                    If zChildCntl.GetType Is GetType(TextBox) Then CType(zChildCntl, TextBox).BackColor = Color.White
                    If zChildCntl.GetType Is GetType(RichTextBox) Then CType(zChildCntl, RichTextBox).Text = ""
                    If zChildCntl.GetType Is GetType(RichTextBox) Then CType(zChildCntl, RichTextBox).BackColor = Color.White
                    If zChildCntl.GetType Is GetType(RadioButton) Then CType(zChildCntl, RadioButton).Checked = False

                Next
            End If
            If zCntl.GetType Is GetType(CheckBox) Then CType(zCntl, CheckBox).Checked = False
            If zCntl.GetType Is GetType(TextBox) Then CType(zCntl, TextBox).Text = ""
            If zCntl.GetType Is GetType(TextBox) Then CType(zCntl, TextBox).BackColor = Color.White
            If zCntl.GetType Is GetType(RichTextBox) Then CType(zCntl, RichTextBox).Text = ""
            If zCntl.GetType Is GetType(RichTextBox) Then CType(zCntl, RichTextBox).BackColor = Color.White
            If zCntl.GetType Is GetType(RadioButton) Then CType(zCntl, RadioButton).Checked = False
            If zCntl.GetType Is GetType(DateTimePicker) Then CType(zCntl, DateTimePicker).Text = Now.Date
            If zCntl.GetType Is GetType(ComboBox) Then CType(zCntl, ComboBox).SelectedIndex = 0

        Next
        Application.DoEvents()

    Catch ex As Exception

    End Try
End Sub
尝试使用子例程将所有控件重置回未使用状态:空白文本框、未选中复选框和单选按钮等

        For Each zCntl As Control In zForm.Controls
            If zCntl.HasChildren Then
                For Each zChildCntl As Control In zCntl.Controls
                    If zChildCntl.GetType Is GetType(CheckBox) Then
                        CType(zChildCntl, CheckBox).Checked = False
                    End If

                    If zChildCntl.GetType Is GetType(TextBox) Then CType(zChildCntl, TextBox).Text = ""
                    If zChildCntl.GetType Is GetType(TextBox) Then CType(zChildCntl, TextBox).BackColor = Color.White
                    If zChildCntl.GetType Is GetType(RichTextBox) Then CType(zChildCntl, RichTextBox).Text = ""
                    If zChildCntl.GetType Is GetType(RichTextBox) Then CType(zChildCntl, RichTextBox).BackColor = Color.White
                    If zChildCntl.GetType Is GetType(RadioButton) Then CType(zChildCntl, RadioButton).Checked = False

                Next
            End If
            If zCntl.GetType Is GetType(CheckBox) Then CType(zCntl, CheckBox).Checked = False
            If zCntl.GetType Is GetType(TextBox) Then CType(zCntl, TextBox).Text = ""
            If zCntl.GetType Is GetType(TextBox) Then CType(zCntl, TextBox).BackColor = Color.White
            If zCntl.GetType Is GetType(RichTextBox) Then CType(zCntl, RichTextBox).Text = ""
            If zCntl.GetType Is GetType(RichTextBox) Then CType(zCntl, RichTextBox).BackColor = Color.White
            If zCntl.GetType Is GetType(RadioButton) Then CType(zCntl, RadioButton).Checked = False
            If zCntl.GetType Is GetType(DateTimePicker) Then CType(zCntl, DateTimePicker).Text = Now.Date
            If zCntl.GetType Is GetType(ComboBox) Then CType(zCntl, ComboBox).SelectedIndex = 0

        Next
        Application.DoEvents()

    Catch ex As Exception

    End Try
End Sub

谢谢你的回答!然而,我的问题一定表述得很糟糕。我的意思是:我已经有办法枚举表单上的控件。但是,此方法仅枚举控件,而不是所有项。我正在搜索一种方法来同时枚举表单上出现的其他项,例如
FolderBrowserDialog
。例如,假设我有一个带有一个标签和一个浏览按钮的表单,该按钮显示FolderBrowserDialog对象。我希望能够列出表单上的所有项目,包括FolderBrowser。谢谢别开玩笑了。您可以通过迭代Me.components.components来迭代表单上的一些组件。但这不会产生对话框对象,它们不会添加到集合中。只有反射才能将它们从窗体对象中挖掘出来。谢谢您的回答!然而,我的问题一定表述得很糟糕。我的意思是:我已经有办法枚举表单上的控件。但是,此方法仅枚举控件,而不是所有项。我正在搜索一种方法来同时枚举表单上出现的其他项,例如
FolderBrowserDialog
。例如,假设我有一个带有一个标签和一个浏览按钮的表单,该按钮显示FolderBrowserDialog对象。我希望能够列出表单上的所有项目,包括FolderBrowser。谢谢别开玩笑了。您可以通过迭代Me.components.components来迭代表单上的一些组件。但这不会产生对话框对象,它们不会添加到集合中。只有反射才能将它们从表单对象中挖掘出来。这似乎不能解决问题,因为OP已经使用了
控件
集合。这似乎不能解决问题,因为OP已经使用了
控件
集合。