How to loop through all controls in a Windows Forms form or how to find if a particular control is a container control?
A simple recursive function should do it.
private void AddEvent(Control parentCtrl)
{
foreach (Control c in parentCtrl.Controls)
{
c.KeyDown += new KeyEventHandler(c_KeyDown);
AddEvent(c);
}
}
How to iterate over windows forms controls
"The name 'text' does not exist in the current context".
text[i].Text you can only do this if
text
type implementsIEnumerable
You can do this to enumerate over your controls
var labels = new List<Label> { label1, label2, label3};
var textBoxs = new List<TextBox> { text1, text2, text3};
for (int i = 1; i <= 5; i++)
{
try
{
tcpCLient.ConnectAsync(textBoxs[i].Text, 80);
labels[i].Text = "Online";
}
catch (Exception)
{
labels[i].Text = "Offline";
}
}
loop over all textboxes in a form, including those inside a groupbox
You can use this function, linq might be a more elegant way.
Dim allTxt As New List(Of Control)
For Each txt As TextBox In FindControlRecursive(allTxt, Me, GetType(TextBox))
'....'
Next
Public Shared Function FindControlRecursive(ByVal list As List(Of Control), ByVal parent As Control, ByVal ctrlType As System.Type) As List(Of Control)
If parent Is Nothing Then Return list
If parent.GetType Is ctrlType Then
list.Add(parent)
End If
For Each child As Control In parent.Controls
FindControlRecursive(list, child, ctrlType)
Next
Return list
End Function
Loop over every X control into a form even if it is nested
This is the exact answer to my question according to the accepted answer of this relevant question which user A Friend pointed on comments. First I have to place this Function
into my Module
:
Public Function FindControlRecursive(ByVal list As List(Of Control), ByVal parent As Control, ByVal ctrlType As System.Type) As List(Of Control)
If parent Is Nothing Then Return list
If parent.GetType Is ctrlType Then
list.Add(parent)
End If
For Each child As Control In parent.Controls
FindControlRecursive(list, child, ctrlType)
Next
Return list
End Function
And then edit my Public Sub
like this:
Public Sub TxtBoxes_Cursors(sender, e)
Dim _FormSender = DirectCast(sender, MetroFramework.Forms.MetroForm)
Dim _MetroTextBoxes As New List(Of Control)
For Each _Textbox As MetroFramework.Controls.MetroTextBox In FindControlRecursive(_MetroTextBoxes, _FormSender, GetType(MetroFramework.Controls.MetroTextBox))
If _Textbox.HasChildren Then
For Each _ClearButton As Control In _Textbox.Controls
If _ClearButton.Name = "lnkClear" Then
_ClearButton.Cursor = System.Windows.Forms.Cursors.Hand
End If
Next
End If
Next
End Sub
C# iterate through the groupbox themselves
Is this what you want? Store the total per GroupBox into List GBtotal. Then you can get the total by summing them.
int Total;
List<int> GBTotal = new List<int>()
foreach(GroupBox gb in Controls.OfType<GroupBox>())
{
int temp = 0;
foreach(TextBox tb in gb.Controls.OfType<TextBox>())
{
int A1 = 0;
if(int.TryParse(tb.Text, out A1))
{
temp += A1;
}
}
GBTotal.Add(temp);
}
Total = GBTotal.Sum();
You technically can use
Total = GBTotal[0] + GBTotal[1] + ....
Or use loop to get the total
Related Topics
Best Way to Find All Factors of a Given Number
Conversion from List<T> to Array T[]
What Is the Use of 'Abstract Override' in C#
Difference Between Delegate.Invoke and Delegate()
How to Find Reason for Generic Gdi+ Error When Saving an Image
The Type Arguments for Method Cannot Be Inferred from the Usage
Iis Server & ASP.NET Core - 500.19 with Error Code 0X8007000D on Httpplatformhandler Tag
Can a Static Method Be Overridden in C#
Deserialize JSON in a "Tryparse" Way
C# Guid and SQL Uniqueidentifier
C# 3.0 Generic Type Inference - Passing a Delegate as a Function Parameter
How to Get All Subsets of an Array
Unzip a Memorystream (Containing the Zip File) and Get the Files