Only text boxes with numbers can be entered (rewrite wndproc)

Source: Internet
Author: User

In our usual use of controls, we often have this requirement-A textbox only allows users to enter numbers, rather than other characters, the common practice is to intercept the keydown event of the textbox and determine whether the input char is a number between 0-9. Today, I want to introduce you to another method, namely, override the wndproc method of the control. This method is simple and accurate. Writing this control is mainly not to introduce textbox that only allows users to enter numbers, but to hope that you can use wndproc to process Windows messages.

Using System;
Using System. Windows. forms;
Namespace Windowscontrollibrary1
{
/**/ ///   <Summary>
/// Only text boxes with numbers can be entered
///   </Summary>
Public   Class Numtextbox: textbox
{
Public Numtextbox ()
{
}

Protected   Override   Void Wndproc ( Ref Message m)
{
Int Wm_char =   Zero X 0102 ;
If (M. msg = Wm_char)
{
If ((( Char ) M. wparam > =   ' 0 ' ) && (( Char ) M. wparam <=   ' 9 ' ) |
( Int ) M. wparam = ( Int ) Keys. Back | ( Int ) M. wparam = ( Int ) Keys. delete)
{
Base . Wndproc ( Ref M );
}
}
Else
{
Base . Wndproc ( Ref m);
}< BR >}

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.