vb.net 在表单中查找以指定字符串开头的控件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19292296/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
vb.net find controls that begins with specified string in a form
提问by illumi
I want to list all names of my buttons that begins with "btn" but these buttons are place in different panels. I have this in my mind
我想列出所有以“btn”开头的按钮名称,但这些按钮位于不同的面板中。我脑子里有这个
dim obj() as object in frmForm.Controls.Find(True,"btn*")
but I think it might be wrong..
但我认为这可能是错误的..
回答by Tim Schmelter
First, the first parameter is the name and the second a boolwhich 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)
回答by TaerAtrin
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. 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:
您可以通过为诸如控制递归之类的东西添加参数来使这更加复杂。请记住,如果您想对它执行任何特定于控件类型的操作(即控件中未从 Control 类继承的任何内容),您需要将该对象作为适当的控件进行 CType。因此,如果 .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

