<Window x:Class="WpfApplication2.Window2" >> xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" >> xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" >> Title="Window2" Height="300" Width="300"> >> <DockPanel> >> <TextBox DockPanel.Dock="Top" x:Name="TB" KeyDown="TextBox_KeyDown"></TextBox> >> <TextBox DockPanel.Dock="Top" x:Name="RealBox" IsEnabled="False"></TextBox> >> </DockPanel> >></Window>Behind:
using System; >>using System.Windows; >>using System.Windows.Input; >> >>namespace WpfApplication2 >>{ >> public partial class Window2 : Window >> { >> public Window2() {InitializeComponent(); } >> >> private DateTime start; >> bool counting; >> >> private void TextBox_KeyDown(object sender, KeyEventArgs e) >> { >> if (counting) >> { >> if (e.Key == Key.OemPlus) >> { >> //End input >> if (DateTime.Now < start.AddSeconds(2) && TB.Text.Length==6) >> { >> RealBox.Text = TB.Text + "+"; >> } >> else >> RealBox.Text = string.Empty; >> counting = false; >> TB.Text = string.Empty; >> e.Handled = true; >> return; >> } >> } >> if (!counting && e.Key == Key.OemPlus) >> { >> start = DateTime.Now; >> counting = true; >> } >> } >> } >>}Only input with a length of 6 characters and starting and ending with a + sign that is entered in less than 2 seconds will get into the 'RealBox'. Obviously you will need to reduce the time check in real life.
<TextBox.InputBindings> <KeyBinding Command="ApplicationCommands.NotACommand" Key="V" Modifiers="Control" /> </TextBox.InputBindings>or, more prettily, in code:
DataObject.AddPastingHandler(TB, (d, e) => { e.CancelCommand(); });