Ralph Maurmeier Ralph Maurmeier - 14 days ago 10
Vb.net Question

Dynamic controls

I have the following function that cycles through controls on a form to store the useful data into a string array. It skips by 2 because the first control is a label. The second control can be a textbox or checkbox. My function allows me to gather all the textbox information but not the checkboxes. How could I modify my function to gather checkbox information? I could use anything like True / False, Checked / Unchecked, etc.

Public Function createInput() As String()
Dim int = myControls.Length
Dim count As Integer = 0
Dim str(int) As String
For i = 1 To int - 1 Step 2
str(count) = myControls(i).Text
count += 1
Next
Return str
End Function

Answer

How could I modify my function to gather checkbox information?

You can use the OfType extension on the Controls collection, assuming myControls is a controls collection:

For Each cb As CheckBox in myControls.OfType(Of CheckBox)

Next