
You want to use LINQ to find specific Windows Forms controls, such as the first matching control. Efficiently find the first focused TextBox. Here we implement a loop on the Windows controls in different ways, using features from the C# programming language.
This C# article compares ways to loop through Windows Forms. It benchmarks query expressions.
LINQ test on Windows Forms controls
Using foreach on Windows Forms was somewhat faster.
Foreach: 780 ms
LINQ version: 889 ms
First, this example shows four different ways of finding the first TextBox control that has focus in a Windows Forms program. The different methods uses LINQ expressions with the query syntax, extension methods, and the foreach-loop in different ways. The pure foreach-loop version (number 4) seems to perform the fastest.
Windows Forms and OfType method [C#]
/// <summary>
/// Use a LINQ query to find the first focused text box on a windows form.
/// </summary>
public TextBox TextBoxFocusedFirst1()
{
var res = from box in this.Controls.OfType<TextBox>()
where box.Focused == true
select box;
return res.First();
}
Using foreach loop [C#]
/// <summary>
/// Use a combination of methods to find the right TextBox.
/// </summary>
public TextBox TextBoxFocusedFirst2()
{
foreach (TextBox t in mainForm.Controls.OfType<TextBox>())
{
if (t.Focused == true)
{
return t;
}
}
}
Using is operator [C#]
/// <summary>
/// Classic code to search form collections.
/// </summary>
public TextBox TextBoxFocusedFirst3()
{
foreach (Control con in mainForm.Controls)
{
if (con is TextBox && con.Focused == true)
{
return con as TextBox;
}
}
}
Using as operator [C#]
/// <summary>
/// Classic code to search form collections.
/// </summary>
public TextBox TextBoxFocusedFirstX()
{
foreach (Control con in mainForm.Controls)
{
if (con.Focused == true)
{
TextBox textBox = con as TextBox;
if (textBox != null)
{
return textBox;
}
}
}
}
Example 1 description. First we see the LINQ version. It uses the var implicit type keyword for simpler syntax. This is just one of many different ways to accomplish the task.
Example 2 description. LINQ allows you to write database-like queries on various objects, including Windows Form controls. We would like to test the performance penalty with LINQ, so here is a different version of our method.
Example 3 description. Here we look at a method that doesn't use LINQ. It uses the equivalent old-style code, which tests every control for whether it is a TextBox and has focus.
Example 4 description. After taking the above benchmarks, I did a bit more tinkering and I found an even better version. This version will avoid one cast and performs about ten percent better than the second version. Note that it doesn't use any LINQ.
Here we saw ways to use LINQ on Windows Forms controls. However, avoiding the LINQ syntax and OfType is faster. So, my recommendation is this: don't use LINQ just because it is there. However, if it makes your life easier, it doesn't have a large penalty. You can find more information on the TextBox control, which was tested using these queries, on this site.
TextBox Tutorial Windows Forms