循环浏览VB表单上的所有组合框

时间:2016-05-11 14:05:53

标签: vb.net visual-studio controls

我正在为一个学校项目做一个成绩申请,我想知道我如何循环并检查某个表格上所有组合框中的值,我有19个单位要检查;试图提高效率而不做出19个案例陈述。我试过一个数组和me.controls。

4 个答案:

答案 0 :(得分:1)

试试这个:

For Each c As Control In Me.Controls.OfType(Of ComboBox)()
    'You cann acces to ComboBox her by c
Next

答案 1 :(得分:1)

请注意,如果按容器(TabControl,SPlitPanel等)控制主机,则可能找不到所有控件。

这是一个递归调用,应该返回所有控件:

Sub GetControlList(container As Control, ByVal ctlList As List(Of Control))
    For Each child As Control In container.Controls
        ctlList.Add(child)
        If (child.HasChildren) Then
            GetControlList(child, ctlList)
        End If
    Next
End Sub

我偶尔会遇到SplitContainer面板上的控件问题,因此如果您使用拆分器,请确保获得所有控件。

获得完整的控件列表后,您就可以对它们进行操作了。此示例正在使用DataGridView控件:

Dim ctrls As New List(Of Control)
GetControlList(Me, ctrls)
For Each dgv As DataGridView In ctrls.OfType(Of DataGridView)()
    AddHandler dgv.DataError, AddressOf DataGridView_DataError
    Debug.Print(dgv.Name)
Next

FYI通用数据错误代码:

Private Sub DataGridView_DataError(sender As Object, e As DataGridViewDataErrorEventArgs)
    Dim dgv As DataGridView = sender
    Dim sGridName As String = dgv.Name.Replace("DataGridView", "")
    Dim col As DataGridViewColumn = dgv.Columns(e.ColumnIndex)
    Dim sColName As String = col.HeaderText
    MsgBox(sGridName & vbNewLine & "Column " & sColName & vbNewLine & e.Exception.Message, MsgBoxStyle.Exclamation)
End Sub

答案 2 :(得分:1)

您已拥有OfType(OF T)方法。你这样使用它:

ForEach box As ComboBox In MyForm.Controls.OfType(Of ComboBox)()

Next

但这只会检查您控件的直接子项。如果您有容器控件,如GroupBox,Panels,FlowControlLayoutPanel等,您将错过嵌套在其中的控件。但我们可以构建一个新的OfType()方法来递归搜索这些:

Public Module ControlExtensions
    <Extension()> 
    Public Iterator Function OfTypeRecursive(Of T As Control)(ByVal Controls As ControlCollection) As IEnumerable(Of T)
        For Each parent As Control In Controls
            If parent.HasChildren Then
                For Each child As Control In OfTypeRecursive(Of T)(parent.Controls)
                    Yield child
                Next child
            End If
        Next parent

        For Each item As Control In Controls.OfType(Of T)()
            Yield item
        Next item
    End Function
End Module

你以同样的方式使用它:

ForEach box As ComboBox In MyForm.Controls.OfTypeRecursive(Of ComboBox)()

Next

答案 3 :(得分:0)

你可能想要检查容器中是否有你正在寻找的类型的控件,这里有一个小功能应该可以帮到你。

Private Function GetControls(Of T)(container As Control, searchChildren As Boolean) As T()
    Dim Controls As New List(Of T)

    For Each Child As Control In container.Controls
        If TypeOf Child Is T Then
            DirectCast(Controls, IList).Add(Child)
        End If

        If searchChildren AndAlso Child.HasChildren Then
            Controls.AddRange(GetControls(Of T)(Child, True))
        End If
    Next

    Return Controls.ToArray()
End Function

以下是用法,如果搜索子项为True,则将搜索所有子容器以查找您正在查找的控件。此外,对于最顶层的容器,假设您从表单中调用代码,我们只会传入Me,否则您可以传递Form实例或特定的Panel,GroupBox等。

Dim ComboBoxes As ComboBox() = GetControls(Of ComboBox)(Me, True)