TheDeveloperBlog.com

Home | Contact Us

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

C# Focused Property

This C# tutorial shows how to use the Focused property. The code loops to find the focused control.

Focused. A TextBox has a Focused property.

Many commands require us to know which TextBox is focused. These include text insertion, copy and paste, cut and undo, and find in this page. We develop methods to find the focused TextBox.

TextBox

Example. Every TextBox or control has a Focused property. This is set to true and false depending on the state of the TextBox. Here we show a method that loops through all the controls on a form and returns the TextBox that has focus.

Bool

Implementation 1: C#

private TextBox TextFocusedFirstLoop()
{
    // Look through all the controls on this form.
    foreach (Control con in this.Controls)
    {
	// Every control has a Focused property.
	if (con.Focused == true)
	{
	    // Try to cast the control to a TextBox.
	    TextBox textBox = con as TextBox;
	    if (textBox != null)
	    {
		return textBox; // We have a TextBox that has focus.
	    }
	}
    }
    return null; // No suitable TextBox was found.
}

private void SolutionExampleLoop()
{
    TextBox textBox = TextFocusedFirstLoop();
    if (textBox != null)
    {
	// We have the focused TextBox.
	// ... We can modify or check parts of it.
    }
}

Result
    Focused control is returned.

In this example, TextFocusedFirstLoop loops through every Control in the this.Controls collection on the Form. Next Focused is checked—every Control is checked to see if it is focused.

Foreach

The as-operator tries to cast an object to another type. With it we try to cast to the TextBox type. If the object cannot be cast, it is set to null. Finally the TextBox is returned.

And: If the focused control is a TextBox, it returns it. If nothing matches, it returns null.

AsNull

Example 2. The code we showed above works well and is reliable, and in rapid application development, it may even be better. But here we present a method that is customized for each program and works faster overall.

Also: It is simpler to understand and shorter. But it must be carefully maintained.

Implementation 2: C#

private TextBox TextFocusedFirst()
{
    // mainBox2 is a TextBox control.
    if (mainBox2.Focused == true)
    {
	return mainBox2;
    }
    // titleBox2 is a TextBox control.
    if (titleBox2.Focused == true)
    {
	return titleBox2;
    }
    // passwordTextBox is a TextBox control.
    if (passwordTextBox.Focused == true)
    {
	return passwordTextBox;
    }
    return null; // Nothing is focused, so return null.
}

private void SolutionExample()
{
    TextBox textBox = TextFocusedFirst();
    if (textBox != null)
    {
	// We have the focused TextBox.
	// ... We can modify or check parts of it.
    }
}

Result
    Focused control is returned.

In this example, each TextBox control is separately tested. This gives us more precision and allows us to specify a list of TextBox controls we want to test. But this method must be updated for each program and each new TextBox added.

Note: There is no loop in TextFocusedFirst so it avoids iterating over controls that are not TextBox objects.

Benchmark. Let's test both methods shown above. The second method is shorter and simpler, and benchmarks are shown next. This approach wins my recommendation. The performance difference is not dramatic but it may be larger on more complex programs.

Loop method

10218
18431
35621
21423 [Average]

Manual check method

7653
15358
22653
15221 [Average]

Summary. It is clear that the method that the second method shown in this document is the faster one. There is a tradeoff, however, and in a few situations it might make sense to choose the loop method instead.


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