TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C# Query Windows Forms Controls

This C# article compares ways to loop through Windows Forms. It benchmarks query expressions.

Query controls. Windows Forms controls can be searched.

This is often done with a foreach-loop. Another option uses LINQ—a query expression can locate the control. There is a performance difference with these approaches. The foreach-loop is faster.

LINQ test on Windows Forms controls

Foreach:      780 ms
LINQ version: 889 ms

Methods. First, this example shows four ways of finding the first TextBox control that has focus in a Windows Forms program. The methods use LINQ expressions with the query syntax, extension methods, and the foreach-loop.

Note: The pure foreach-loop version (number 4) seems to perform the fastest in my tests.

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 code version. It uses the var implicit type keyword for simpler syntax. This is just one of many different ways to accomplish the task.

Var

Example 2: LINQ allows you to write database-like queries on various objects, including Windows Form controls.

Note: 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. This is imperative code.

After taking the above benchmarks, I did a bit more tinkering and I found an even better version (4). This version will avoid one cast and performs about ten percent better than the second version. Note that it uses no LINQ.

Summary. We saw ways to use LINQ on Windows Forms controls. Avoiding the LINQ syntax and OfType is faster. So, my recommendation is this: don't use LINQ just because it is there. But if it makes your life easier, it doesn't have a large penalty.

TextBox: You can find more information on the TextBox control, which was tested using these queries.

TextBox


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf