2

我正在翻译我的 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对象不是controls.

有没有办法让我遍历表单上显示的所有项目?

非常感谢!

CFP

4

2 回答 2

1

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

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

这将为您提供对话框的窗口句柄。接下来,您必须迭代对话框的子窗口。您可以使用 EnumChildWindows API 调用来做到这一点。这为您提供了每个孩子的窗口句柄,然后您可以使用 SendMessage() 对孩子做一些事情。无论那可能是什么,您都没有在问题中指定。

于 2010-01-30T13:58:58.437 回答
-2
 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
于 2014-05-14T18:11:54.003 回答