+ 1
C# Finding typeof(UserControl) in a Collection
I was wondering if there is a simplification - if there is one. https://code.sololearn.com/c697M7ArID0s/?ref=app I am indexing through a list of UserControls. I want to find certain Custom UserControls by datatype. This line is invalid. I am wondering if there is something similar so that I don't have to use a for loop. int index = controls.IndexOf(typeof(Navigation));
5 Antworten
+ 3
Your are looking for the "is" operator.
Console.WriteLine("The are {0} controls in the controlsList", controlsList.Count);foreach(UserControl item in controlsList){ if(item is Navigation) { Console.WriteLine("This is a navigation user control"); item.Visible = true; } else { Console.WriteLine("This is not a navigation user control"); item.Visible = false; }}
+ 1
Can give more information ? What are you trying to do ?
+ 1
Edited my question.
0
Thanks for the answer. That is much simpler, and that foreach is probably faster than a for-loop and if-statement.