【问题标题】:Making Numpad in c# winforms not working在 c# winforms 中制作 Numpad 不起作用
【发布时间】:2012-08-20 00:19:30
【问题描述】:
private void btnPress_Click(object sender, EventArgs e)
    {
        String key = ((Button)sender).Text;
        InputSimulator.SimulateTextEntry(key);
        SendKeys.SendWait(VirtualKeyCode.NUMPAD4.ToString());
    }

[DllImport("user32.dll")]
    public static extern int SetForegroundWindow(IntPtr hWnd);

    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new KeyPress());
    }

这东西没有任何帮助。当我单击我的文本框并单击 ont button1 时,此处不会出现 1.. 并且文本框焦点也丢失了..

【问题讨论】:

  • 您可以将最后聚焦的文本框保存在 textBox_GotFocus 事件中,并在 btnPress_Click 中简单地附加到其文本。
  • 但你不认为这是一种糟糕的编码方法。有那么难做吗?
  • @Sirwani - 你能解释一下按钮点击里面的代码吗?准确地说是第二行和第三行。
  • 如果 button1 被按下,则文本为 1,然后将 1 存储在“key”中,然后尝试将此文本模拟到文本框中
  • SendKeys.SendWait(VirtualKeyCode.NUMPAD4.ToString());只是网络上的另一种热门和试用方式

标签: c# winforms keyboard keyboard-events


【解决方案1】:

如果我得到你,你需要类似的东西

private TextBox _lastFocused; defined in your form

然后将 OnEnter 或 OnExit 添加到每个参与的文本框设置 _lastFocused,这样您就知道您希望按键转到哪个控件

然后在你的 buttonClick(s) 中

String key = ((Button)sender).Text;
_lastFocused.Focus();
InputSimulator.SimulateTextEntry(key);
SendKeys.SendWait(VirtualKeyCode.NUMPAD4.ToString()); 
((Button)sender).Focus(); // if you want to out focus back the button clicked

据我所知,正在发生的事情是 单击按钮会将焦点从您所在的任何文本框转移到按钮。 Sendkeys 将您的模拟按键路由到当前聚焦的控件,这当然是您的按钮。所以解决方法就是将注意力转移回你上次来的地方。

【讨论】:

  • 有很多文本框.. 对于每个文本框我都必须这样做,这将使我的工作更加艰难。如果单击的事件是按钮或类似的东西,则会有一些东西可以获取当前光标位置并且必须更改焦点
猜你喜欢
  • 1970-01-01
  • 2011-06-21
  • 2014-12-25
  • 1970-01-01
  • 2013-03-25
  • 2018-01-18
  • 2015-05-01
  • 1970-01-01
  • 2016-09-12
相关资源
最近更新 更多