Method 1: Private Void Tbox_keypress ( Object Sender, keypresseventargs e ){ If (E. keychar = 0x20 ) E. keychar = ( Char ) 0 ; // Disable Space key If (E. keychar = 0x2d ) & (Textbox) sender). Text. Length =0 )) Return ; // Process negative numbers If (E. keychar> 0x20 ){ Try { Double . Parse (textbox) sender). Text + E. keychar. tostring ());} Catch {E. keychar = (Char ) 0 ; // Handle invalid characters }}} Method 2: Private Void Textbox_keypress ( Object Sender, keypresseventargs e ){ If (E. keychar! = 8 &&! Char. isdigit (E. keychar) {e. Handled = True ;} Or Private Void Textbox_keypress ( Object Sender, keypresseventargs e ){ If (E. keychar! = ' \ B ' &&! Char. isdigit (E. keychar) {e. Handled = True ;} Method 3: Private Void Textbox1_keypress (Object Sender, system. Windows. Forms. keypresseventargs e ){ If (E. keychar! = ' \ B ' ) // This is the allowed backspace key { If (E. keychar < ' 0 ' ) | (E. keychar> ' 9 ' )) // This is a 0-9 number. {E. Handled = True ; }}} Method 4: Private Void Textbox1_validating ( Object Sender, canceleventargs e ){ Const String Pattern = @" ^ \ D + \.? \ D + $ " ; String Content = (Textbox) sender). text; If (! (RegEx. ismatch (content, pattern) {errorprovider1.seterror (Control) sender, " Only numbers are allowed! " ); E. Cancel = True ;} Else Errorprovider1.seterror (Control) sender, Null );} Method 5: Private Void Textbox1_keypress ( Object Sender, system. Windows. Forms. keypresseventargs e ){ If (E. keychar = ' . ' && This . Textbox1.text. indexof ( " . " )! =- 1 ) {E. Handled = True ;} If (! (E. keychar> = 48 & Amp; E. keychar <= 57 ) | E. keychar = ' . ' | E. keychar = 8 ) {E. Handled = True ;} Method 6: Private Void Tbx_lsregcapital_keypress ( Object Sender, keypresseventargs e ){ If (! Char. isnumber (E. keychar )&&! Char. ispunctuation (E. keychar )&&! Char. iscontrol (E. keychar) {e. Handled = True ; // Remove inappropriate characters } Else If (Char. ispunctuation (E. keychar )){ If (E. keychar! = ' . ' | This . Textbox1.text. Length = 0 ) // Decimal point {E. Handled = True ;} If (Textbox1.text. lastindexof ( ' . ' )! =- 1 ) {E. Handled = True ; }}} Method 7: ASCII code solution ,{ If (E. keychar <= 48 | E. keychar> = 57 ) & (E. keychar! = 8 ) & (E. keychar! = 46 ) E. Handled = True ; ====================== 48 represents 0, 57 represents 9, 8 represents space, 46 represents the decimal point}