Jack Jack - 11 days ago 5
C# Question

How to force the form focus?

How can I force the focus of an form?

.Focus()
is not working for me.

private void button1_Click(object sender, EventArgs e) {
var form = new loginForm();
if (Application.OpenForms[form.Name] == null) {
form.Show();
} else {
form.Focus();
}
}


What am I doing wrong?

Answer

it should be

private void button1_Click(object sender, EventArgs e) {
   var form = new loginForm();
    if (Application.OpenForms[form.Name] == null) {
           form.Show();
    } else {
         Application.OpenForms[form.Name].Focus();
    }
}