2009-04-17 57 views
0

我在Form对象的OnKeyDown中收到按键。当满足一些条件时(例如键击是可打印字符而不是热键),我想将键转发到表单上的文本控件,并将焦点设置为文本控件,以便用户可以继续输入。我能够使用MapVirtualKey对输入的字符进行解码,但我只能得到“未移位”字符(总是大写)。使用ToUnicodeEx看起来像太多的PITA。在WinForms中将按键转发到另一个控件

这样做的最好方法是什么?没有办法简单地转发Windows消息本身吗?

难道我不能拦截ProcessKeyPreview或其他类似的东西,并将其转发给文本控件的ProcessKeyPreview?任何类似的思路?

凹凸:没有答案!

回答

1

我这样做有以下

private void MyFunkyForm_KeyDown(object sender, KeyEventArgs e) 
{ 
    // to avoid duplicate letters in textbox :-) 
    if (textBox2.Focused == false) 
    { 

     // get the char from the Keycode 
     char inputChar = (char)e.KeyCode; 
     if (char.IsLetterOrDigit(inputChar)) 
     { 
      // if letter or number then add it to the textbox 
      textBox2.Text += inputChar; 
     } 

     // set focus 
     textBox2.Focus(); 
     // set cursor to the end of text, no selection 
     textBox2.SelectionStart = textBox2.Text.Length; 
    } 
} 
单一形式
相关问题