School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 300,347 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,530 people online right now. Registration is fast and FREE... Join Now!




Create numbers only TextBox with VB.NET

 
Reply to this topicStart new topic

> Create numbers only TextBox with VB.NET

AdamSpeight2008
Group Icon



post 6 Jan, 2009 - 05:55 PM
Post #1


Convert any textbox to numbers only.

On many occasions in the forums this is often asked, so here is a solution that makes the process simpler.

Define a new public class in your project called NumberOnlyTextbox

CODE

Public Class NumberOnlyTextbox
Inherits TextBox
Protected m_ctrl As TextBox
Protected m_throw As KeyEventHandler
Protected m_AllowMinus As Boolean
Protected m_AllowDecimalsPoint As Boolean


To allow this capability to happen we need to supply a new instance of this class with a few details;-
The Textbox Control
Are decimal points allowed?
Are minus allowed?
And the AddressOf of a subroutine to handle to allowed keypressed. (Using Nothing if you don't provide one)
CODE

Public Sub New(ByRef ctrl As TextBox, ByRef AllowDecimalsPoint As Boolean, ByRef AllowMinus As Boolean, ByRef throwb As KeyEventHandler)
  ' Pass a reference to the textbox
  m_ctrl = ctrl
  ' Are Decimal Points allowed through
  m_AllowDecimalsPoint = AllowDecimalsPoint
  ' Are minus allowed through
  m_AllowMinus = AllowMinus
  ' Pass a reference to the delegate. (This is AddressOf a sub which further process the keypress.)
  m_throw = throwb
  ' Add an handler for the keydown event
  AddHandler m_ctrl.KeyDown, AddressOf M_OnKeyDown
End Sub


The next sub handles the keydown event, filtering them. Passing them on the supplied sub.
CODE

Protected Sub M_OnKeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs)
  e.SuppressKeyPress = False
  Select Case e.KeyCode
   ' Allow Digit keys through.
   Case Keys.Decimal, Keys.OemPeriod

    If m_AllowDecimalsPoint Then
     If m_throw IsNot Nothing Then m_throw.Invoke(sender, e)
    End If
   Case Keys.Subtract, Keys.OemMinus
    If m_AllowMinus Then
     If m_throw IsNot Nothing Then m_throw.Invoke(sender, e)
    End If
   Case Keys.D0 To Keys.D9, Keys.NumPad0 To Keys.NumPad9
    If m_throw IsNot Nothing Then m_throw.Invoke(sender, e)
   Case Keys.Delete, Keys.Back, Keys.Enter, Keys.Return, Keys.Left, Keys.Right, Keys.Tab
    ' Allow "STANDARD" editting keys
    If m_throw IsNot Nothing Then m_throw.Invoke(sender, e)
   Case Else
    ' Supress all other key.
    e.Handled = True
    e.SuppressKeyPress = True
  End Select
End Sub

The following routine removes the event handler previously defined during initialization.
CODE

Protected Overrides Sub Finalize()
  ' Remember to remove the handle
  RemoveHandler m_ctrl.KeyDown, AddressOf M_OnKeyDown
  MyBase.Finalize()
End Sub
End Class




Using the class

Place a textbox on to a form, Form1 & TextBox1 in this example.
Then add the following code to the form's code.
CODE

Public Class Form1
Dim a As NumberOnlyTextbox

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  a = New NumberOnlyTextbox(Me.TextBox1, True, True, AddressOf M_OnKeyDown)
End Sub
Private Sub M_OnKeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs)
  Console.WriteLine(e.KeyCode)

End Sub
End Class


Run. See what you can enter in textbox.

Note: That it is still possible to create malformed numbers but detecting this is left as a requirement for the developer.
Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!

PsychoCoder
Group Icon



post 7 Jan, 2009 - 02:34 PM
Post #2
Nice tutorial Adam (Seen this asked quite a few times lately). Hope you don't mind, I changed the title to be a little better descriptive of the content of the tutorial smile.gif )
Go to the top of the page
+Quote Post


Fast ReplyReply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 


Lo-Fi Version Time is now: 11/7/09 06:04PM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month