Page 1 of 1

Create numbers only TextBox with VB.NET Rate Topic: -----

#1 AdamSpeight2008   User is offline

  • MrCupOfT
  • member icon

Reputation: 2298
  • View blog
  • Posts: 9,535
  • Joined: 29-May 08

Posted 06 January 2009 - 06:55 PM

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

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)
 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.
 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.
 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.
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.

Is This A Good Question/Topic? 0
  • +

Replies To: Create numbers only TextBox with VB.NET

#2 PsychoCoder   User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1663
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Posted 07 January 2009 - 03:34 PM

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 :) )
Was This Post Helpful? 0
  • +
  • -

#3 AdamSpeight2008   User is offline

  • MrCupOfT
  • member icon

Reputation: 2298
  • View blog
  • Posts: 9,535
  • Joined: 29-May 08

Posted 20 October 2011 - 12:53 PM

NuGet: NumbersOnly
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1