简体   繁体   English

vb.net查找以表格中指定字符串开头的控件

[英]vb.net find controls that begins with specified string in a form

I want to list all names of my buttons that begins with "btn" but these buttons are place in different panels. 我想列出所有以“ btn”开头的按钮的名称,但是这些按钮位于不同的面板中。 I have this in my mind 我脑子里有这个

dim obj() as object in frmForm.Controls.Find(True,"btn*")

but I think it might be wrong.. 但我认为这可能是错误的。

First, the first parameter is the name and the second a bool which indicates whether you want to search recursively or not. 首先,第一个参数是名称,第二个参数是bool ,它指示您是否要递归搜索。

Second, there is no builtin way for this. 第二,没有内置的方法。 I would use your own method, one like this: 我将使用您自己的方法,如下所示:

Public Function FindControlStartsWith(root As Control, name As String, recursive As Boolean, comparison As StringComparison) As Control()
    If root Is Nothing Then
        Throw New ArgumentNullException("root")
    End If
    Dim controls = New List(Of Control)
    Dim stack = New Stack(Of Control)()
    stack.Push(root)

    While stack.Count > 0
        Dim c As Control = stack.Pop()
        If c.Name.StartsWith(name, comparison) Then
            controls.Add(c)
        End If
        If recursive Then
            For Each child As Control In root.Controls
                stack.Push(child)
            Next
        End If
    End While
    Return controls.ToArray()
End Function

Use it in this way: 以这种方式使用它:

Dim obj() As Control = FindControlStartsWith(Me, "BUT", True, StringComparison.OrdinalIgnoreCase)

I do something similar with the type of control, but it can easily be modified for name. 我对控件的类型进行了类似的操作,但是可以很容易地对其名称进行修改。 Try the code below: 请尝试以下代码:

Private Sub findcontrols(ByVal root As Control)

    For Each cntrl As Control In root.Controls
        findcontrols(cntrl)
        If cntrl.name.startswith("btn") Then
            msgbox(cntrl.name)
        End If


End Sub

You can make this even more complex by adding parameters for stuff like controlling recursion and such. 您可以通过为诸如控制递归之类的东西添加参数来使其更加复杂。 Keep in mind that if you want to do any control type-specific stuff with it (ie. anything that is in the control that is not inherited from the Control class), you need to CType that object as the appropriate control. 请记住,如果要使用它来处理任何特定于控件类型的东西(即控件中不从Control类继承的任何东西),则需要将该对象CType作为适当的控件。 So, if .name was only in the Button class, and did not exist in the Control class, I would have to do the following for this to work: 因此,如果.name仅在Button类中,而在Control类中不存在,则必须执行以下操作才能起作用:

msgbox(ctype(cntrl, Button).name)

My own personal version of it looks more like this: 我自己的个人版本看起来像这样:

Private Sub clearcontrols(ByVal root As Control, ByVal ClearLists As Boolean, Optional ByVal ClearTabPages As Boolean = False)
    For Each cntrl As Control In root.Controls
        clearcontrols(cntrl, ClearLists, ClearTabPages)

        If TypeOf cntrl Is TextBox  Then
            CType(cntrl, TextBox).Clear()
        End If

        If TypeOf cntrl Is DataGridView Then
                CType(cntrl, DataGridView).Rows.Clear()
        End If

        If TypeOf cntrl Is ListBox   And ClearLists = True Then
                CType(cntrl, ListBox).Items.Clear()
        End If

        If TypeOf cntrl Is TabControl And ClearTabPages = True Then
            For Each tp As TabPage In CType(cntrl, TabControl).TabPages
                If DynTPList.Contains(tp.Name) Then
                    tp.Dispose()

                End If

            Next
        End If
    Next

End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM