Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ability to find WinForm control via the Tag property

Tags:

c#

winforms

I am working in C# on an existing WinForm project. The original code uses Tag to convey hardware addressing info for a bunch of textboxes that represent certain hardware registers in a connected microcontroller system. I know how to find an unknown control by searching for its Name using the Control.ControlCollection.Find method, but it's unclear to me on whether I can find the control by the Tag (just a string in this instance).

like image 353
M D Avatar asked Nov 04 '13 19:11

M D


1 Answers

You can use LINQ to find controls based on Tag

var items = parentControl.ControlCollection;
var item = items.Cast<Control>().FirstOrDefault(control => String.Equals(control.Tag, tagName));
like image 165
Tilak Avatar answered Dec 02 '22 18:12

Tilak