Custom textbox that can only enter numbers in WPF

Source: Internet
Author: User

Keydown event: optimize tab and rightctrl

   Private     Void  Tbcount_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 | E. Key. tostring () = " Tab " )
{
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 ;
If (E. Key. tostring () ! = " Rightctrl " )
{
MessageBox. Show ( This . Resources [ " Txt_innerpage_connpointmanage_tabmyconnpoint_addmycloudseenum_prompt " ]. Tostring (), This . Resources [ " Txt_innerpage_connpointmanage_tabmyconnpoint_addmycloudseenum_prompttitle " ]. Tostring (), messageboxbutton. OK, messageboximage. Warning );
}
}
}

Textchanged event:

 

 Private    Void  Textbox_textchanged (  Object  Sender, textchangedeventargs E)
{
// Block Chinese Input and illegal character paste Input
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.