nu2vbdotnet
Member
- Joined
- Jan 30, 2007
- Messages
- 7
- Programming Experience
- Beginner
Gurus,
I need your help. I am trying to create a customised user control namely a numberbox. This "numberbox" inherits all properties from a standard TextBox. This numberbox should only take in numeric entries and check for it (I am using regular expression for this, see code below) when the focus leaves the control.
Currently according to this bit of code it rejects all alphabetic entries but accepts all numeric entries and alphanumeric entries.
Can I get help on how to use regular expression for checking numeric entries in this case? My project involves using a lot Textboxes with numeric entries..such a method will help reduce the code I believe..
Any help is highly appreciated..!
--------------------------------------------------------------------
Imports System.text.RegularExpressions
Imports System.Windows.Forms
Imports System.Drawing
Public Class numberbox
Inherits TextBox
Public Sub New()
Me.BackColor = Color.White
Me.ForeColor = Color.Black
End Sub
Private Function IsNumeric(ByVal txt As String) As Boolean
Dim IsNum As Boolean = False
If Regex.IsMatch(txt, "\d") Then
IsNum = True
End If
Return IsNum
End Function
Protected Overrides Sub OnLeave(ByVal e As System.EventArgs)
If Not IsNumeric(Me.Text) Then
MessageBox.Show("Please Enter Numeric Values Only", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
Me.BackColor = Color.Red
Me.Focus()
Me.SelectAll()
Else
Me.BackColor = Color.White
End If
End Sub
End Class