C# Focused Property

Use the Focused property. See example code that 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

Info: TextFocusedFirstLoop loops through every Control in the this.Controls collection on the Form. Every Control is checked to see if it is focused.


Cast: The as-operator tries to cast an object to another type. We try to cast to the TextBox type. If the object cannot be cast, it is set to null.

Finally: The TextBox is returned. If the focused control is a TextBox, it returns it. If nothing matches, it returns null.

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. } }

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.

Here: In this example, each TextBox control is separately tested. 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.

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. } }

Benchmark. Let's test both methods. 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 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.
Dot Net Perls
© 2007-2020 Sam Allen. Every person is special and unique. Send bug reports to