Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 272
Jack Avatar asked Dec 11 '11 00:12

Jack


People also ask

What does focus () do in C#?

The Focus method returns true if the control successfully received input focus. The control can have the input focus while not displaying any visual cues of having the focus. This behavior is primarily observed by the nonselectable controls listed below, or any controls derived from them.

How do you focus an open form in C#?

When showing a form that contains a TextBox, it's common courtesy to focus the TextBox so that the user can begin typing immediately. To focus a TextBox when a Windows Form first loads, simply set the TabIndex for the TextBox to zero (or the lowest TabIndex for any Control on the Form).


1 Answers

Try this:

this.BringToFront();
this.Activate();
like image 70
Adriano Patricio Avatar answered Sep 18 '22 11:09

Adriano Patricio