Revision: 24177
Initial Code
Initial URL
Initial Description
Initial Title
Initial Tags
Initial Language
at February 22, 2010 16:35 by heatherk
Initial Code
Option Strict On
Imports System.Windows.Forms
Public Class NumberOnlyTextBox
Inherits System.Windows.Forms.TextBox
Protected Overrides Sub OnKeyDown(ByVal e As System.Windows.Forms.KeyEventArgs)
Select Case e.KeyCode
Case Keys.D0, Keys.D1, Keys.D2, Keys.D3, Keys.D4, Keys.D5, Keys.D6, Keys.D7, _
Keys.D8, Keys.D9, Keys.NumPad0, Keys.NumPad1, Keys.NumPad2, Keys.NumPad3, _
Keys.NumPad4, Keys.NumPad5, Keys.NumPad6, Keys.NumPad7, Keys.NumPad8, _
Keys.NumPad9
'number pressed
If e.Shift And e.KeyCode <> Keys.D8 Then
'multiplication symbol still allowed
e.SuppressKeyPress = True
e.Handled = True
Else
MyBase.OnKeyDown(e)
End If
Case Keys.Add, Keys.Subtract, Keys.Divide, Keys.Multiply, Keys.Decimal, _
Keys.Oemplus, Keys.OemPeriod, Keys.OemMinus, Keys.OemQuestion
'arithmetic symbol
If e.Shift And (e.KeyCode = Keys.OemMinus Or e.KeyCode = Keys.OemQuestion) Then
e.SuppressKeyPress = True
e.Handled = True
Else
MyBase.OnKeyDown(e)
End If
Case Keys.Back, Keys.Back, Keys.Enter, Keys.Escape, Keys.Delete, Keys.Insert, Keys.Home, _
Keys.End, Keys.F1, Keys.F2, Keys.F3, Keys.F4, Keys.F5, Keys.F6, Keys.F7, _
Keys.F8, Keys.F9, Keys.F10, Keys.F11, Keys.F12, Keys.Tab, Keys.Up, _
Keys.Down, Keys.Left, Keys.Right, Keys.Shift, Keys.ShiftKey, Keys.Control, _
Keys.ControlKey, Keys.Alt
'misc keys that we still want to view events for
MyBase.OnKeyDown(e)
Case Else
'bad key, supress keystroke
e.SuppressKeyPress = True
e.Handled = True
End Select
End Sub
End Class
Initial URL
Initial Description
NumberTextBox is a control used to restrict input to numbers, function keys and arithmetic symbols only. Otherwise, it works just like a normal TextBox.
Initial Title
Numbers-Only Text Box
Initial Tags
forms
Initial Language
VB.NET