C # Enter and paste numbers in the wpf winfrom control text box

Source: Internet
Author: User
Code

 Private void txtMachineCode_KeyDown (object sender, KeyEventArgs e)
{
TextBox txt = sender as TextBox;

// Shield illegal buttons
If (e. Key> = Key. NumPad0 & e. Key <= Key. NumPad9) | e. Key = Key. Decimal)
{
If (txt. Text. Contains (".") & e. Key = Key. Decimal)
{
E. Handled = true;
Return;
}
E. Handled = false;
}
Else if (e. key> = Key. d0 & e. key <= Key. d9) | e. key = Key. oemPeriod) & e. keyboardDevice. modifiers! = ModifierKeys. Shift)
{
If (txt. Text. Contains (".") & e. Key = Key. OemPeriod)
{
E. Handled = true;
Return;
}
E. Handled = false;
}
Else
{
E. Handled = true;
}
}
Private void txtMachineCode_TextChanged (object sender, TextChangedEventArgs e)
{
// Block Chinese Input and illegal character paste Input
TextBox textBox = sender as TextBox;
TextChange [] change = new TextChange [e. Changes. Count];
E. Changes. CopyTo (change, 0 );

Int offset = change [0]. Offset;
If (change [0]. AddedLength> 0)
{
Double num = 0;
If (! Double. TryParse (textBox. Text, out num ))
{
TextBox. Text = textBox. Text. Remove (offset, change [0]. AddedLength );
TextBox. Select (offset, 0 );
}
}
}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.