Is there efficient way to filter Panel's child controls by their type e.g. Label
property e.g. Tag
value?
E.g. I have a panel1:
label1.Tag=1;
label2.Tag=1;
label3.Tag=2;
label4.Tag=3;
textBox1.Tag=1;
panel1.Add(controls above);
I would like to get all labels in the collection, or all the controls with Tag=1
, or use and
between the statements.
You can filter all controls of a specific type using the OfType
extension method:
var labelControls = panel.Controls.OfType<Label>();
Then if you want to add additional filtering (e.g. based on the tag):
var filteredLabelControls = labelControls.Where(l => l.Tag == (object)1);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With