在WPF文本框上模拟键盘事件

时间:2010-07-15 09:16:06

标签: wpf button textbox sendkeys

我的页面中有文本框和4个按钮(A,B,删除和回车)。如果我点击按钮,它必须将键事件发送到文本框。

问题:  文本框中没有任何操作。

代码:

void buttonElement_Click(object sender, RoutedEventArgs e)
    {
        // create variable for holding string
        String sendString = "";           
            // stop all event handling
            e.Handled = true;

            // set sendstring to key
            sendString = ((Button)sender).CommandParameter.ToString();                              

            // if something to send
            if (!String.IsNullOrEmpty(sendString))
            {
                // if sending a string
                if (sendString.Length > 1)
                {
                    // add {}
                    sendString = "{" + sendString + "}";
                }

                    // set keyboard focus
                System.Windows.Input.Keyboard.Focus(this.txtSearch);                                                         
               System.Windows.Forms.SendKeys.SendWait(sendString);

            }           
    }

格塔。

2 个答案:

答案 0 :(得分:0)

为什么要尝试将键事件发送到TextBox,而不是设置其Text属性?

答案 1 :(得分:0)

Daniel Rose是对的。这样会不会更容易? 你获取文本框的Text属性,然后在按下按钮时,将右侧字符追加到按下删除按钮的字符串中,只删除该字符串的最后一个字符。

相关问题