【问题标题】:auto press enter C# when textbox value changed as event当文本框值更改为事件时自动按 Enter C#
【发布时间】:2014-01-17 08:52:40
【问题描述】:

我开发了一个应用程序,我想在按下 Enter 后运行一些代码,当我按下 Enter 时代码成功运行。我想要一种在文本框值更改时自动按 Enter 的方法。

但作为事件,因为我的线程有问题,所以如果我在键盘上按 Enter,它运行良好,但是当我编写行代码 (sendkey.send("{Enter}")) 时,它不像我按下那样运行Enter 在键盘上 我想在按 Enter 时运行方法“image()”

private void tbResponse_TextChanged(object sender, EventArgs e)
    {

        SendKeys.Send("{ENTER}");

    }

    private void tbResponse_KeyPress(object sender, KeyPressEventArgs e)
    {

        if (e.KeyChar == (char)13)
        {
            image();
        }
    }

【问题讨论】:

  • 为什么要尝试在自己的应用程序中模拟键盘?只需调用适当的方法即可。
  • 旁注 - 我们在 C# 中将 PascalNames 用于方法和类型名称
  • 不,我想在输入后调用方法,因为我在条形码阅读器上工作,第一次扫描是条形码,第二次“输入后”是图像扫描
  • 当我直接调用该方法时我不工作并且条形码和应用程​​序停止工作当它改变时我按 Enter tne 方法运行我认为它因为线程而停止

标签: c# .net winforms


【解决方案1】:

直接调用image()方法,无需任何按钮“自动按下”:

private void tbResponse_TextChanged(object sender, EventArgs e)
{
    image();
}

private void tbResponse_KeyPress(object sender, KeyPressEventArgs e)
{
    if (e.KeyChar == (char)13)        
        image();        
}

【讨论】:

    【解决方案2】:

    @Sergey Berezovskiy 解决方案很好,但如果您仍想使用按键事件处理它,请这样做

    private void tbResponse_TextChanged(object sender, EventArgs e)
    {
    
        tbResponse_KeyPress(this, new KeyPressEventArgs((char)13));
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多