簡體   English   中英

在C#Winforms中使Numpad無法正常工作

[英]Making Numpad in c# winforms not working

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());
    }

我的表格布局

請這東西沒有任何幫助。 當我單擊我的文本框並單擊on button1時,那么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將模擬的按鍵路由到當前集中的控件,當然這是您的按鈕。 因此,解決方法只是將焦點移回到上次來自的位置。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM