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

C# 在.net中自动关闭虚拟键盘,c#,winforms,C#,Winforms,我正在使用windows窗体应用程序,同时单击文本框我想启用虚拟键盘,因此我在我的TxtName\u GotFocus事件中编写了以下代码 private void button1_Click(object sender, EventArgs e) { System.Diagnostics.Process.Start("osk.exe"); //SetFocus to your TextBox here textBox1.Focus(); } 但是我想在TxtName\

我正在使用windows窗体应用程序,同时单击文本框我想启用虚拟键盘,因此我在我的
TxtName\u GotFocus
事件中编写了以下代码

private void button1_Click(object sender, EventArgs e)
{
    System.Diagnostics.Process.Start("osk.exe");
    //SetFocus to your TextBox here
    textBox1.Focus();
}
但是我想在
TxtName\u LostFocus中关闭这个虚拟键盘

那么如何编写代码呢?

订阅文本框的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();
}

这看起来像是
vb.net
而不是
c
进程。Start
返回一个对象。不要扔掉它。显示错误:无法处理请求,因为进程已退出。我不是OP,但idea很有趣,所以我已经测试并确认了它。但我什么也记不住,只能投上一票。
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();
}