即使它是嵌套的,也可以将每个X控件循环到一个表单中(Loop over every X control into a form even if it is nested)

编程入门 行业动态 更新时间:2024-10-27 20:29:51
即使它是嵌套的,也可以将每个X控件循环到一个表单中(Loop over every X control into a form even if it is nested)

在安装了Metroframework Modern UI 1.4.0.0VB.NET项目中,我将这部分代码放入一个模块中,以便将所有MetroTextBox放入MetroTabControl ,并将Cursor.Hand给它们的嵌入式Clear按钮 。 我怎么能做同样的事情,包括可能不在MetroTextBox es,进入我的表单或其他容器,例如? 换句话说,我想将每个MetroTextBox循环到一个表单中,即使它是嵌套的。

Public Sub TxtBoxes_Cursors(sender, e) Dim _FormSender = DirectCast(sender, MetroFramework.Forms.MetroForm) For Each _MetroTabControl As Control In _FormSender.Controls.OfType(Of MetroFramework.Controls.MetroTabControl)() For Each _TabPage As Control In _MetroTabControl.Controls.OfType(Of MetroFramework.Controls.MetroTabPage)() For Each _Textbox As Control In _TabPage.Controls.OfType(Of MetroFramework.Controls.MetroTextBox)() If _Textbox.HasChildren Then For Each _ClearButton As Control In _Textbox.Controls If _ClearButton.Name = "lnkClear" Then _ClearButton.Cursor = System.Windows.Forms.Cursors.Hand End If Next End If Next Next Next End Sub

On a VB.NET project with Metroframework Modern UI 1.4.0.0 installed, I use this part of code into a Module to loop over all MetroTextBoxes which placed into a MetroTabControl and give Cursor.Hand to their embedded Clear button. How can I do the same thing including MetroTextBoxes which could be out of MetroTabControl, into my form or into an other container for example? In other words, I would like to loop over every MetroTextBox into a form, even if it is nested.

Public Sub TxtBoxes_Cursors(sender, e) Dim _FormSender = DirectCast(sender, MetroFramework.Forms.MetroForm) For Each _MetroTabControl As Control In _FormSender.Controls.OfType(Of MetroFramework.Controls.MetroTabControl)() For Each _TabPage As Control In _MetroTabControl.Controls.OfType(Of MetroFramework.Controls.MetroTabPage)() For Each _Textbox As Control In _TabPage.Controls.OfType(Of MetroFramework.Controls.MetroTextBox)() If _Textbox.HasChildren Then For Each _ClearButton As Control In _Textbox.Controls If _ClearButton.Name = "lnkClear" Then _ClearButton.Cursor = System.Windows.Forms.Cursors.Hand End If Next End If Next Next Next End Sub

最满意答案

根据这位相关问题的接受答案, 这位 朋友指出了哪位用户的评论,这是我的问题的确切答案。 首先,我必须将此Function放入我的Module :

Public Function FindControlRecursive(ByVal list As List(Of Control), ByVal parent As Control, ByVal ctrlType As System.Type) As List(Of Control) If parent Is Nothing Then Return list If parent.GetType Is ctrlType Then list.Add(parent) End If For Each child As Control In parent.Controls FindControlRecursive(list, child, ctrlType) Next Return list End Function

然后编辑我的Public Sub像这样:

Public Sub TxtBoxes_Cursors(sender, e) Dim _FormSender = DirectCast(sender, MetroFramework.Forms.MetroForm) Dim _MetroTextBoxes As New List(Of Control) For Each _Textbox As MetroFramework.Controls.MetroTextBox In FindControlRecursive(_MetroTextBoxes, _FormSender, GetType(MetroFramework.Controls.MetroTextBox)) If _Textbox.HasChildren Then For Each _ClearButton As Control In _Textbox.Controls If _ClearButton.Name = "lnkClear" Then _ClearButton.Cursor = System.Windows.Forms.Cursors.Hand End If Next End If Next End Sub

This is the exact answer to my question according to the accepted answer of this relevant question which user A Friend pointed on comments. First I have to place this Function into my Module:

Public Function FindControlRecursive(ByVal list As List(Of Control), ByVal parent As Control, ByVal ctrlType As System.Type) As List(Of Control) If parent Is Nothing Then Return list If parent.GetType Is ctrlType Then list.Add(parent) End If For Each child As Control In parent.Controls FindControlRecursive(list, child, ctrlType) Next Return list End Function

And then edit my Public Sub like this:

Public Sub TxtBoxes_Cursors(sender, e) Dim _FormSender = DirectCast(sender, MetroFramework.Forms.MetroForm) Dim _MetroTextBoxes As New List(Of Control) For Each _Textbox As MetroFramework.Controls.MetroTextBox In FindControlRecursive(_MetroTextBoxes, _FormSender, GetType(MetroFramework.Controls.MetroTextBox)) If _Textbox.HasChildren Then For Each _ClearButton As Control In _Textbox.Controls If _ClearButton.Name = "lnkClear" Then _ClearButton.Cursor = System.Windows.Forms.Cursors.Hand End If Next End If Next End Sub

更多推荐

本文发布于:2023-08-02 07:13:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1370057.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:嵌套   它是   表单   控件   form

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!