A Form can hold one or more controls in a
Collection[
^]. You can see this by looking at the
Form.Designer.vb class file. For example:
ComboBox1 = New ComboBox()
Button1 = New Button()
TextBox1 = New TextBox()
ComboBox2 = New ComboBox()
SuspendLayout()
ComboBox1.FormattingEnabled = True
ComboBox1.Location = New Point(300, 49)
ComboBox1.Name = "ComboBox1"
ComboBox1.Size = New Size(104, 23)
ComboBox1.TabIndex = 0
Button1.Location = New Point(349, 108)
Button1.Name = "Button1"
Button1.Size = New Size(119, 27)
Button1.TabIndex = 1
Button1.Text = "Button1"
Button1.UseVisualStyleBackColor = True
TextBox1.Location = New Point(357, 160)
TextBox1.Name = "TextBox1"
TextBox1.Size = New Size(91, 23)
TextBox1.TabIndex = 2
ComboBox2.FormattingEnabled = True
ComboBox2.Location = New Point(348, 214)
ComboBox2.Name = "ComboBox2"
ComboBox2.Size = New Size(104, 23)
ComboBox2.TabIndex = 3
AutoScaleDimensions = New SizeF(7.0F, 15.0F)
AutoScaleMode = AutoScaleMode.Font
ClientSize = New Size(800, 450)
Controls.Add(ComboBox2)
Controls.Add(TextBox1)
Controls.Add(Button1)
Controls.Add(ComboBox1)
Name = "Form1"
Text = "Form1"
ResumeLayout(False)
PerformLayout()
So we can see when a control is placed on the form, the VS IDE creates the code automatically. Each control is added to the
Form1.Controls
collection property.
So we need to iterate over this
Collection[
^] property to access the controls on the form. We can also filter the collection to only return a specific type by using
Linq[
^] and the
OfType[
^] extension method:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
For Each comboBox As ComboBox In Me.Controls.OfType(Of ComboBox)
Debug.WriteLine(comboBox.Name)
Next
End Sub
The code above will iterate over only the ComboBoxes on the form, and will display their names in the Locals window. Replace my code with yours.
UPDATE
Here is Microsoft's implementation of the
OfType
method:
public static IEnumerable<TResult> OfType<TResult>(this IEnumerable source)
{
if (source == null)
{
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source);
}
return OfTypeIterator<TResult>(source);
}
private static IEnumerable<TResult> OfTypeIterator<TResult>(IEnumerable source)
{
foreach (object? obj in source)
{
if (obj is TResult result)
{
yield return result;
}
}
}
Here is a VB translation of the Microsoft code above:
Public Shared Function OfType(Of TResult)(ByVal source As IEnumerable) _
As IEnumerable(Of TResult)
If source Is Nothing Then
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source)
End If
Return OfTypeIterator(Of TResult)(source)
End Function
Private Shared Iterator Function OfTypeIterator(Of TResult)(ByVal source As IEnumerable) _
As IEnumerable(Of TResult)
For Each obj As Object In source
If TypeOf obj Is TResult Then
Dim result As TResult = DirectCast(obj, TResult)
Yield result
End If
Next
End Function
Here you can see the test is the same as the code that @CHill60 has posted, but as a generic test:
If TypeOf obj Is TResult Then
End If
So,
TResult
is
ComboBox
and
obj
is the control in the form's
Controls
collection.