Public class numericTextBox inherits system.windows.Forms.TextBox
Private Sub NumericTextBox_KeyPress (ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles MyBase.KeyPress' knockdown KeyPress message '{' determines whether the input is a number, if not the number is returned If (Not Char.IsDigit ( E.KEYCHAR) Then '{e.handled = true' If the input is not a number, the system declaration the keypress event has been processed, ignoring the 'system to the KeyPress event continues to process.
'} Else' {e.handled = false 'If the input is a number, continue to let the system handle the keypress message to display the character'} end if '} End Subend Class
You can use this class instead of your system.form.TextboxTextBox class; principle is from System.Windows.Forms.TextBox to derive a class, then intercept its keypress event; Key, simply report that KeyPress has been processed; if it is a numeric key, release the system processing .or first processed keypree event if not (isnumeric (E.Keychar) or E.Keychar = chrw (8) OR E . Keychar = chrw (46)) THEN E.HandLED = True End IF is replication paste event [DLLIMPORT ("User32.dll", charset = charset.auto)] public static extern int getWindowlong (HandleRef hwnd, int NINDEX); [DllImport ( "user32.dll", CharSet = CharSet.Auto)] public static extern int SetWindowLong (HandleRef hWnd, int nIndex, int dwNewLong); public const int GWL_STYLE = -16; public const int ES_NUMBER = 0x2000;
public static void TextBoxNumberOnly (TextBox textBox, bool readOnly) {int style = GetWindowLong (new HandleRef (textBox, textBox.Handle), GWL_STYLE); if (readOnly) style | = ES_NUMBER; else style & = ~ ES_NUMBER; SetWindowLong (new HandleRef (TextBox, TextBox.handle), GWL_Style, Style;