
You want to find the focused TextBox. There are many commands that require to know what the focused TextBox is, and whether any TextBox is focused. These include text insertion, copy and paste, cut and undo, and find in this page. We see some methods you can use to find the focused TextBox in your C# program that uses Windows Forms.
TextBox TutorialThis C# tutorial shows how to use the Focused property. The code loops to find the focused control.
First, we note that every TextBox or control has a Focused property. This is set to true and false depending on the state of the TextBox. In the first example, we show a method that loops through all the controls on a form and returns the TextBox that has focus.
Bool TypeImplementation 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.
Description. The first method in the 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 Loop Examples
As cast is used. The as operator tries to cast an object to another type. Above, we try to cast to the TextBox type. If the object cannot be cast, it is set to null. Finally, TextBox is returned. If the focused control is a TextBox, return it. If nothing matches, return null.
As Cast Example Null TipsThe 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. It is also simpler to understand and shorter. However, it must be maintained more carefully.
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.
Each TextBox is tested. Every TextBox control is tested separately. This gives us more precision and allows us to specify a list of TextBox controls we want to test. However, this method must be updated for each program and each new TextBox added.
Does not loop. There is no loop in TextFocusedFirst so it avoids iterating over controls that are not TextBox objects. This is often a big performance improvement.

Let's benchmark 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 really 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]

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. Here the method approach itself is more important than the benchmark.
Windows Forms