Reputation: 277
I have created a number of controls that are derived from base controls. As part of these extended controls I have added a number of properties. One of these properties is a unique ID that helps me to bind it to a database value.
I need to be able to search for this control by UniqueID, a property that only my derived controls have (note that all controls on the form are my derived controls, and all of them have UniqueID as a property). Reflection jumps to mind but I cannot find an example.
Upvotes: 2
Views: 1100
Reputation: 223392
Use Enumerable.OfType<T>
to filter out controls of your specific type and then you can query against the specific property, something like:
var controls = this.Controls.OfType<YourControl>().Where(r => r.UniqueId == someValue);
Remember this only searches the controls at root level, if you are interested in finding nested controls then you have to use a recursive approach. See: How to get ALL child controls of a Windows Forms form of a specific type (Button/Textbox)?
Upvotes: 2