在安装了Metroframework Modern UI 1.4.0.0的VB.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 SubOn 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 SubThis 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 FunctionAnd 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更多推荐
发布评论