简体   繁体   English

为什么表格无法获得焦点?

[英]Why Won't Form Get Focus?

I have a form that is launched modally like this: 我有一个这样模态启动的表单:

private void find_street_Click(object sender, EventArgs e)
{
  this.WindowState = FormWindowState.Minimized;
  Form findForm = new FindStreet();
  findForm.ShowDialog();
  this.WindowState = FormWindowState.Normal;
}

The form launches correctly, and the cursor is in the first text box, whose TabIndex is set to 1. 窗体正确启动,并且光标在第一个文本框中,其TabIndex设置为1。

Along with the InitializeComponent(); 连同InitializeComponent(); call, these commands are present. 呼叫,这些命令存在。

public FindStreet()
{
  InitializeComponent();
  this.TopMost = true;
  this.BringToFront();
  this.Focus();
}

I have looked at and tried a number of examples. 我查看并尝试了许多示例。 The cursor appears in the correct control, but the form's window does not have the focus. 光标出现在正确的控件中,但是窗体的窗口没有焦点。 The problem is that if a user starts typing, even though the newly launched form is visible, those keystrokes are not going into the text box. 问题是,即使可见新启动的表单,如果用户开始键入,这些击键也不会进入文本框。

Remove the code in public FindStreet() and in load event of FindStreet add: 删除公共FindStreet()中的代码,并在FindStreet的 load事件中添加:

this.TopMost = true; //i don't know why you need this.
this.Activate();

When you minimize your main form the next one in z-order get the cursor. 当最小化主窗体时,z顺序的下一个窗体将获得光标。 this.Focus() doesn't do anything. this.Focus()不执行任何操作。 You need to Activate the dialog. 您需要激活对话框。

A dialog requires an owner, that cannot be a minimized window. 对话框需要一个所有者,该所有者不能是最小化的窗口。 Now accidents start to happen, starting with your WindowState assignment. 现在开始发生事故,从您的WindowState分配开始。 Your app doesn't have a window left that can receive the focus so Windows is forced to find another one, that will be one owned by another application. 您的应用程序没有剩余的窗口可以接收焦点,因此Windows被迫寻找另一个窗口,该窗口将归另一个应用程序所有。 Same problem happens when you close the dialog. 当您关闭对话框时,也会发生相同的问题。

You can still get the intended effect, you must hide your main window after the dialog is displayed, show it again before the dialog closes. 您仍然可以获得预期的效果,必须显示对话框之后隐藏主窗口, 然后在对话框关闭之前再次显示它。 That requires a bit of hackorama: 这需要一点hackorama:

    using (var dlg = FindStreet()) {
        // Show main window when dialog is closing
        dlg.FormClosing += new FormClosingEventHandler((s, cea) => {
            if (!cea.Cancel) this.Show();
        });
        // Hide main window after dialog is shown
        this.BeginInvoke(new Action(() => {
            this.Hide();
        }));
        dlg.StartPosition = FormStartPosition.Manual;
        dlg.Location = this.Location;
        if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK) {
            // etc...
        }
    }

And remove the hacks from the FindStreet constructor. 并从FindStreet构造函数中删除这些hack。 Watch out for event order if you have a FormClosing event handler in FindStreet, be sure to override OnFormClosing() instead. 如果在FindStreet中具有FormClosing事件处理程序,请注意事件顺序,请确保改写OnFormClosing()。

If you want to set a specific control as the current active control then try this: 如果要将特定控件设置为当前活动控件,请尝试以下操作:

this.ActiveControl = myTextBox;

This will place the cursor you want as the main focus, when the form loads. 加载表单时,这会将您要放置的光标作为主要焦点。 So try this out: 所以试试看:

public FindStreet()
{
  InitializeComponent();
  this.TopMost = true;
  this.BringToFront();
  this.Focus();
  this.ActiveControl = myTextBox;
}

Here is the link to Focus() which should explain why your focus call was not working. 这是Focus()的链接,该链接应解释为什么焦点调用无法正常工作。

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

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