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

发布于 2024-08-19 12:48:34 字数 798 浏览 9 评论 0原文

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

有没有办法让我真正循环浏览表单上显示的所有项目?

多谢!

财务规划师

I'm translating my VB.Net application, and I need to loop through all the controls on my form. Using a recursive function such as

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

works very well, but it doesn't include CommonDialog objects, such as FolderBrowser objects. How can I access these objects ? I tried this

    For Each ChildDialog As CommonDialog In Ctrl.Controls
        ChildDialog.Tag = Translate(ChildDialog.Tag)
    Next

But there is obviously an inheritance problem, since CommonDialog objects are not controls.

Is there a way for me to loop through really all the items displayed on my form?

Thanks a lot!

CFP

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

荒岛晴空 2024-08-26 12:48:34

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

您遇到的第一个问题是当显示这样的对话框时运行您的代码。它是一个对话框,在 ShowDialog() 调用之后,控制不会返回到您的程序,直到用户关闭它。通过相当多的技巧,这是可能的。检查我的代码此线程 的方法。如前所述,该代码适用于任何 shell 对话框以及 MessageBox。

这将为您提供对话框的窗口句柄。接下来,您必须迭代对话框的子窗口。您可以通过 EnumChildWindows API 调用来完成此操作。这为您提供了每个子项的窗口句柄,然后您可以使用 SendMessage() 对子项执行某些操作。无论是什么,您都没有在问题中具体说明。

No, they are components, not controls. Their code actually lives in the shell, they were written in unmanaged C/C++ by Microsoft. The only thing that's managed about them is a small wrapper that makes the necessary API calls to display them and return their result. OpenFileDialog for example.

The very first problem you'll run into is run your code when such a dialog is displayed. It is a dialog, control doesn't return to your program after the ShowDialog() call until the user dismisses it. It is possible with a fair amount of trickery. Check my code in this thread for the approach. As noted, that code will work for any shell dialog, as well as MessageBox.

That gets you the window handle of the dialog. Next, you have to iterate the child windows of the dialog. You can do that with the EnumChildWindows API call. That gives you the window handle of each child, you can then use SendMessage() to do something with the child. Whatever that might be, you didn't specify that in your question.

庆幸我还是我 2024-08-26 12:48:34
 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
 Friend Sub resetFormControls(zForm As Form)

Try a subroutine to reset all controls back to non-used state: blank text box, unchecked checkbox and radiobutton, etc.

        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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文