在.net中自动关闭虚拟键盘

时间:2014-02-05 07:31:16

标签: c# winforms

我正在使用Windows窗体应用程序,而单击文本框我想启用虚拟键盘,所以我在TxtName_GotFocus事件中编写了以下代码

private void button1_Click(object sender, EventArgs e)
{
    System.Diagnostics.Process.Start("osk.exe");
    //SetFocus to your TextBox here
    textBox1.Focus();
}

但我想关闭TxtName_LostFocus中的虚拟键盘 那我怎么写代码呢?

2 个答案:

答案 0 :(得分:1)

订阅文本框的LostFocus事件:

Process keyboardProcess;
private void button1_Click(object sender, EventArgs e)
{
    this.keyboardProcess = System.Diagnostics.Process.Start("osk.exe");
    //SetFocus to your TextBox here
    textBox1.Focus();
}

private void textbox1_LostFocus(object sender, EventArgs e)
{
    this.keyboardProcess.Kill();
}

答案 1 :(得分:1)

private void button1_Click(object sender, EventArgs e)
{
    System.Diagnostics.Process.Start("osk.exe");
    //SetFocus to your TextBox here
    textBox1.Focus();
}

private void textbox1_LostFocus(object sender, EventArgs e)
{
    var procs = Process.GetProcessesByName("osk");
    if (procs.Length != 0)
        procs[0].Kill();
}