How can I get the textbox inside a For:
I have a For that runs 20 times and I have 20 textboxes on my screen, I would like to know how to get all textboxes containing name get the text and play in an array.
How can I get the textbox inside a For:
I have a For that runs 20 times and I have 20 textboxes on my screen, I would like to know how to get all textboxes containing name get the text and play in an array.
You do not need to use for
for this, you can simply use a Linq query to simplify this.
See an example:
var textos = this.Controls.OfType<TextBox>().
Where(control => control.Name.Contains("Publicacao")).
Select(txt => txt.Text).ToArray();
This query takes from the Controls
property of the form, all controls that are TextBox
(or derived from it) whose name contains the Publication literal > and then select only the text of the same ones, throwing them in the string array ( string[]
) textos
.
You can use the property Controls of the form and run a linq to get the controls with the desired name.
List<TextBox> textBoxes = this.Controls.OfType<TextBox>()
.Where(ctrl => ctrl.Name.Contains("Publicacao")).ToList();
You can also use a for by traversing the controls that are inside the panel. In this function you pass your dashboard, and you will scroll through all the TextBox, and those that have the name "Publication" will be saved in a list of names, and returned.
Public Function NomesArray(ByRef painel As Control) As List(Of String)
Dim ArrayNomes As New List(Of String)
For Each Control In painel.Controls
If TypeOf Control Is TextBox Then
If DirectCast(Control, txtBusca).Name = "Publicacao" Then
ArrayNomes.Add(DirectCast(Control, txtBusca).Name)
End If
End If
Next
Return ArrayNomes
End Function