应用程序到达前台后,文本框无法获得焦点

时间:2014-07-31 09:14:16

标签: c# textbox

我的应用程序在系统托盘中最小化。 在特定的击键时,应用程序出现在前面。 (感谢How can I bring my application window to the front?

在应用程序的主屏幕上有一个类型来搜索'一种文本框。 用户必须在这里开始输入。

但是,即使我的应用程序出现在前面,后台应用程序仍然会收到击键,并且在文本框中没有输入任何内容。

我已尝试过textbox.focus(),textbox.Select(),form.activecontrol = textbox,但似乎没有任何效果。

以下是我的代码的外观:

    gkh.HookedKeys.Add(Keys.Oemtilde);
    gkh.HookedKeys.Add(Keys.ControlKey);
    gkh.HookedKeys.Add(Keys.Escape);
    gkh.KeyUp += new KeyEventHandler(gkh_KeyUp);


    void gkh_KeyUp(object sender, KeyEventArgs e)
    {
        if ((Keys.Oemtilde == e.KeyCode) && (Keys.Control == Control.ModifierKeys))
        {
            // Set the WindowState to normal
            this.WindowState = FormWindowState.Normal;
            //Bring the main tab to display
            tabControl1.SelectTab(tabPage_Search);

            this.TopMost = true;
            this.Focus();
            this.BringToFront();
            this.TopMost = false;

            this.textBox_Search.Select();
            this.textBox_Search.SelectionStart = 0;
            this.textBox_Search.Focus();
        }
        else if (Keys.Escape == e.KeyCode) 
        {
            //Minimize to system tray when Escape key is pressed.
            this.WindowState = FormWindowState.Minimized;
        }
        else
        {
            //NOP - Intentionally kept empty
        }
        e.Handled = true;
    }

0 个答案:

没有答案