Trying to raiseEvent to textBox - but i don't see the key value in the textBox text. I can see that the textbox event "OnKeyDownEvent" is stopping in breakpoints - but i don't understand why the text of the KeyEventArgs ( Key.D0 ) is not insert into the textbox text.
The code:
if( currentTextBoxInFocus != null )
{
KeyEventArgs k = new KeyEventArgs( Keyboard.PrimaryDevice, Keyboard.PrimaryDevice.ActiveSource, System.Environment.ProcessorCount, Key.D0 );
//KeyEventArgs k = new KeyEventArgs( Keyboard.PrimaryDevice, Keyboard.PrimaryDevice.ActiveSource, 0, Key.D0 );
k.RoutedEvent = UIElement.KeyDownEvent;
currentTextBoxInFocus.RaiseEvent( k );
k.RoutedEvent = UIElement.KeyUpEvent;
currentTextBoxInF开发者_运维百科ocus.RaiseEvent( k );
}
You're trying to use the Key events as the source of input, when they are actually used to indicate the result of keyboard input from the OS. These events don't cause text to be added to a TextBox. They instead indicate when keyboard input has been entered into a control (like the TextBox).
If you want to simulate typing into a TextBox from code just add the desired text into the Text property:
int caret = currentTextBoxInFocus.CaretIndex;
currentTextBoxInFocus.Text = String.Format("{0}0{1}", currentTextBoxInFocus.Text.Substring(0, caret), currentTextBoxInFocus.Text.Substring(currentTextBoxInFocus.CaretIndex));
currentTextBoxInFocus.CaretIndex = caret + 1;
精彩评论