简体   繁体   English

C#新形式从未引起关注

[英]C# New form never gains focus

I have been trying to write a small app with its own option windows. 我一直在尝试编写一个带有自己的选项窗口的小型应用程序。 When I try to launch the window I can never seem to set focus on the new form. 当我尝试启动窗口时,我似乎永远无法集中精力于新表单。 This is not a mdi form, but merely a new form that I create when a user selects an option from the menu. 这不是mdi表单,而只是我在用户从菜单中选择一个选项时创建的新表单。 It should be noted that Form.Show is return false, which means that the new form is never receiving focus. 应当注意,Form.Show为false,这意味着新窗体永远不会受到关注。

I have tried multiple methods for loading the form and all have failed: 我尝试了多种方法来加载表单,但都失败了:

From Calling 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 Form to selectable: 将表格设置为可选:

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

Using Old API: 使用旧的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

Passing in Parent 传递父母

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

In all of these cases the form will show up, but the form that spawned still will have focus over the new form. 在所有这些情况下,表单都会显示出来,但是生成的表单仍将重点放在新表单上。 This happens even when I disable the old form before I create the new form. 即使在创建新表单之前禁用了旧表单,也会发生这种情况。

Any suggestions? 有什么建议么?

It's because Form.canFocus() is false when the form loads. 这是因为加载表单时Form.canFocus()为false。 Use Form.Activate() on Form.Shown event. Form.Shown事件上使用Form.Activate() That's all. 就这样。

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

I solved with this (thanks to @Joel Coehoorn ): 我解决了这个(感谢@Joel Coehoorn ):

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

Set TopMost form property to true. TopMost窗体属性设置为true。 Then 然后

in program.cs: 在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;

}

Try to call ShowDialog(this) . 尝试调用ShowDialog(this) It helped in my case when I faced the same problem. 当我遇到相同的问题时,这对我来说很有帮助。

Remember that there is only a single user interface thread allowed in a winforms app. 请记住,winforms应用程序只允许一个用户界面线程。

Are you manipulating anything on the parent form after your call to Form.Show() ? 调用Form.Show() 之后,您是否要在父表单上进行任何操作? This may cause the parent form to be focused again. 这可能会导致父窗体再次被聚焦。

Remove everything you have used to try to focus, activate the form and rely just on the call to Form.Show() . 删除您尝试聚焦的所有内容,激活表单并仅依赖于对Form.Show()的调用。 This should be enough to load the form, and focus upon it. 这应该足以加载表单并专注于表单。 If anything, in your menu item handler. 如果有的话,在菜单项处理程序中。 Comment out everything after your call to Show() and see if that works. 调用Show()之后,注释掉所有内容,看看是否可行。 Work backwards to see what caused your parent form to be refocused. 向后工作,看看是什么原因导致您的父表单重新聚焦。

This seems to work. 这似乎有效。 First I create the 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 form I do the following: 然后在新表单的构造函数中,执行以下操作:

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

Apparently there is some sort of behind the scene difference between Form.Show and Form.ShowDialog. 显然,Form.Show和Form.ShowDialog之间存在某种幕后差异。 Not quites sure what it is, I can only think it has to do with setting the active parent somehow. 不太确定它是什么,我只能认为这与以某种方式设置活动父项有关。 Adding code after the call to construct the function does not seem to give back focus to the parent form. 在构造函数的调用之后添加代码似乎不会使焦点重新指向父窗体。 Which it shouldn't. 它不应该。

Have you tried setting the correct parent window in Form.Show() ? 您是否尝试过在Form.Show()设置正确的父窗口?

Eg: 例如:

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

Edit: 编辑:

There's something going on that isn't in your question. 发生的事情不在您的问题中。 Is your owning window a TopMost window? 您拥有的窗口是TopMost窗口吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM