I have a c# windows forms application with several textboxes and a button. I would like to find out the textbox which has focus and do something with it. I have written following code but of course it won't work because the button will get focus as soon as it is pressed.
private void button1_MouseDown(object sender, MouseEventArgs e)
{
foreach (Control t in this.Controls)
{
if (t is TextBox)
{
if (t.Focused)
{
MessageBox.Show(t.Name);
}
}
}
}
See Question&Answers more detail:os