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 >}