C # New form never gets focused

I am trying to write a small application with my own options windows. When I try to launch the window, I can never focus on the new form. This is not an mdi form, but just a new form that I create when the user selects an option from the menu. It should be noted that Form.Show returns false, which means that the new form never gets focus.

I tried several methods for loading the form and all failed:

From the call form:

ServerForm SF = new ServerForm(ref DataLoader, false);
SF.Show();
SF.Focus();
// Fails

      

Inside the form itself:

this.Show();
this.BringToFront();
this.Activate();
this.TopMost = true;
// Fails

      

Setting up the form for selection:

this.SetStyle(System.Windows.Forms.ControlStyles.Selectable, true);
...
ServerForm SF = new ServerForm(ref DataLoader, false);
SF.Show();
SF.Focus();
// Fails

      

Using the old API:

[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern int ShowWindow(IntPtr handle, int nCmdShow);    
ServerForm SF = new ServerForm(ref DataLoader, false);
ShowWindow(SF.Handle, 3);
SF.Show();
SF.Focus();
// Fails

      

Walkthrough in parent

ServerForm SF = new ServerForm(ref DataLoader, false);
SF.Show(this);
SF.Focus();
// Fails

      

In all these cases, the form will be displayed, but the form that spawned still will focus on the new form. This even happens when I disable the old form before I create the new form.

Any suggestions?

0


a source to share


7 replies


It seems to work. First, I create a new form:

private void changeDefaultServerToolStripMenuItem_Click(object sender, EventArgs e)
{
    this.Enabled = false;
    ServerForm SF = new ServerForm(ref DataLoader, true);
}

      

Then in the constructor for the new view, I do the following:



this.BringToFront();
this.CenterToParent();
this.TopMost = true;
this.ShowDialog();

      

Apparently there is some kind of difference behind the difference between Form.Show and Form.ShowDialog. Not sure what it is, I can only think it has to do with setting the active parent. Adding code after the call to build the function does not seem to return focus to the parent form. Which shouldn't.

0


a source


This is because it Form.canFocus()

is false when the form is loaded. Use Form.Activate()

in an event Form.Shown

. It's all.



private void ServerForm_Shown(object sender, EventArgs e)
{
    this.Activate();
}

      

+9


a source


Set the form property TopMost

to true. Then

in program.cs:

var formLogin = new frmLogin();
formLogin.ShowDialog();


if (formLogin.DialogResult == DialogResult.Yes)
{
        Application.Run(new frmMain());
}

      

in formLogin:

[DllImport("user32")]
public static extern int SetForegroundWindow(IntPtr hwnd);         

...

private void frmLogin_Shown(object sender, EventArgs e)
{
  SetForegroundWindow(this.Handle);
}

private void frmLogin_Deactivate(object sender, EventArgs e)
{
    TopMost = false;

}

      

+1


a source


I solved with this (thanks @Joel Coehoorn ):

form.WindowState = FormWindowState.Minimized;
form.Shown += delegate(Object sender, EventArgs e) {
    ((Form)sender).WindowState = FormWindowState.Normal;
};
form.ShowDialog();

      

+1


a source


Remember that only one UI thread is allowed in a winforms application.

Do you control anything in the parent form after your call Form.Show()

? This can bring the parent shape back into focus.

Delete whatever you used to try to focus, activate the form, and rely only on the challenge Form.Show()

. This should be enough to load the form and focus on it. Anyway, in the menu item handler. Comment on everything after your call Show()

and see if it works. Work backward to see what caused your parent form to reorient.

0


a source


Try calling ShowDialog(this)

. This helped me in my case when I faced the same problem.

0


a source


Have you tried setting the correct parent window in Form.Show()

?

eg:.

using(ServerForm SF = new ServerForm(ref DataLoader, false)) // if ServerForm is IDisposable
{
    SF.Show(this);
}

      

Edit:

There's something going on there, it's not your question. Is your user window a TopMost window?

-1


a source







All Articles