-1

I am trying to loop through all the combo-boxes on my windows form VB.net application.

I had assumed this would work

Array.ForEach(Me.Controls.OfType(Of ComboBox).Items.Add(DataGridView1.Columns(i).Name)))

but I can not refer to the items it seems to not know it is a combobo at that point

I am trying to get a list of all my combobox names so i can hopefully use that list of names in a loop to add items and read the selected index but my list of names is always blank. I am using the following code just trying to send the list to a messgebox to see if it is grabbing any names.

Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
    Dim allComboBoxValues As String = ""
    Dim c As Control
    Dim childc As Control
    For Each c In Me.Controls
        For Each childc In c.Controls
            If TypeOf childc Is ComboBox Then
                allComboBoxValues &= CType(childc, ComboBox).Text & ","
            End If
        Next
    Next
    MsgBox(allComboBoxValues)

    If allComboBoxValues <> "" Then
        MsgBox(allComboBoxValues)
    End If
End Sub
Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188
Summers
  • 13
  • 2

2 Answers2

1

The bellow function can be used to retrieved all child Controls of a certain type.

Private Function GetAll(Control As Control, Type As Type) As IEnumerable(Of Control)
        Dim Controls = Control.Controls.Cast(Of Control)()
        Return Controls.SelectMany(Function(x) GetAll(x, Type)).Concat(Controls).Where(Function(y) y.GetType = Type)
End Function

Usage:

GetAll(Me, GetType(Combobox))

For your needs:

Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
    Dim Values As String = String.Empty
    For Each ComboBox As ComboBox In GetAll(Me, GetType(ComboBox))
        Values &= ComboBox.Text & ","
    Next
    MsgBox(Values)
End Sub

(Function retrieved from this answer and converted to vb.net)

Quima
  • 894
  • 11
  • 21
0

I use this Extension method. It uses generics which is quite clean. As mentioned in comments, recursion is a necessity.

Public Module ExtensionMethods

    <Extension()>
    Public Function ChildControls(Of T As Control)(ByVal parent As Control) As IEnumerable(Of T)
        Dim result As New List(Of T)
        For Each ctrl As Control In parent.Controls
            If TypeOf ctrl Is T Then result.Add(CType(ctrl, T))
            result.AddRange(ctrl.ChildControls(Of T)())
        Next
        Return result
    End Function

End Module

Usage in your scenario:

Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
    Dim myCombos = Me.ChildControls(Of ComboBox)
    Dim allComboBoxValues = String.Join(", ", myCombos.Select(Function(c) c.Text))
    If myCombos.Any() Then
        MsgBox(allComboBoxValues)
    End If
End Sub
djv
  • 15,168
  • 7
  • 48
  • 72
  • Thank you for your help! this way of doing this will be helpful in other parts of this application. – Summers Dec 26 '17 at 15:41