472,992 Members | 3,271 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,992 software developers and data experts.

TextBoxes that accept only numbers

We have access to Infragistics etc., but we would like to make use of a
regular VS textbox so that it accepts only numbers. We have discovered that
if you bind an integer field to a textbox, for example, the textbox will
only accept numbers. This is nice but what do you do about nulls? It does
not seem to accept nulls. What do you do about literals? (like commas in
100,000). Is there a white paper somwhere that delineates all the issues?
Is there a custom control already written somewhere that we could take
advantage of?

Any pointers, comments or suggestions appreciated.

Nov 21 '05 #1
6 2451
Hi Woody,

I've actually written a control for this purpose (I wrote a different, but
similar, one for currency only). Below is the code. Once you build the
solution, you have to add the control in the usual way to vs .net. Then you
may need some help as to why I created 2 new events - leavex and keypressx,
and if you do, just email me back with any questions at
be*****@cherwellinc.com.

I can send you the .dll itself, but this actually should be easier, as the
code is pretty brief and straightforward. Moreover, you can modify it to
accept commas, as I did not include that.

Bernie Yaeger
Imports System.ComponentModel

Public Class textboxnumbersonly

Inherits System.Windows.Forms.TextBox

Public Event leavex()

Public Event keypressx(ByVal e As System.Windows.Forms.KeyPressEventArgs)

Sub New()

MyBase.New()

Me.Text = "0"

Me.TextAlign = System.Windows.Forms.HorizontalAlignment.Right

End Sub

Protected Overrides Sub OnKeyPress(ByVal e As
System.Windows.Forms.KeyPressEventArgs)

Dim KeyAscii As Integer = Asc(e.KeyChar)

If Me.Text.Length > 0 And KeyAscii = 45 And Me.SelectionStart <> 0 Then

e.Handled = True

RaiseEvent keypressx(e)

Exit Sub

End If

If InStr(Me.Text, "-") And KeyAscii = 45 Then

e.Handled = True

RaiseEvent keypressx(e)

Exit Sub

End If

Select Case KeyAscii

Case 48 To 57, 45, 8, 27, 9 ' 46 is . 45 is - 43 is + 8 is backspace

' 27 is escape; delete, arrow keys, home, end are not trapped

' by keypress so they are available

Case Else

KeyAscii = 0

End Select

If KeyAscii = 0 Then

e.Handled = True

Else

e.Handled = False

End If

RaiseEvent keypressx(e)

End Sub

Protected Overrides Sub OnLeave(ByVal e As System.EventArgs)

If Me.Text = "-" Or Me.Text.Length = 0 Then

Me.Text = "0"

End If

RaiseEvent leavex()

End Sub

End Class

"Woody Splawn" <No**********@jts.bz> wrote in message
news:Ok**************@TK2MSFTNGP09.phx.gbl...
We have access to Infragistics etc., but we would like to make use of a
regular VS textbox so that it accepts only numbers. We have discovered
that
if you bind an integer field to a textbox, for example, the textbox will
only accept numbers. This is nice but what do you do about nulls? It
does
not seem to accept nulls. What do you do about literals? (like commas in
100,000). Is there a white paper somwhere that delineates all the issues?
Is there a custom control already written somewhere that we could take
advantage of?

Any pointers, comments or suggestions appreciated.

Nov 21 '05 #2

"Bernie Yaeger" <be*****@cherwellinc.com> wrote
I can send you the .dll itself, but this actually should be easier, as the
code is pretty brief and straightforward. Moreover, you can modify it to
accept commas, as I did not include that.


What do you do about pasting in text from the clipboard?

LFS
Nov 21 '05 #3
Thank you for your input.

Did you see the part in my message regarding nulls? How do you deal with
nulls?

Nov 21 '05 #4
Hi Larry,

That's why I have a leavex event - on leave, if the text does not covert to
a number, it can be reset to zero.

Bernie

"Larry Serflaten" <se*******@usinternet.com> wrote in message
news:ef**************@TK2MSFTNGP12.phx.gbl...

"Bernie Yaeger" <be*****@cherwellinc.com> wrote
I can send you the .dll itself, but this actually should be easier, as
the
code is pretty brief and straightforward. Moreover, you can modify it to
accept commas, as I did not include that.


What do you do about pasting in text from the clipboard?

LFS

Nov 21 '05 #5
Hi Woody,

I did see that comment, but I kind of glossed over it. I assume the textbox
is filled by a person entering keystrokes. If this is so, then you can
leave it blank when the data should be null, mark it blank in the leavex
event, and deal with that accordingly (blank diff from 0). If however the
data is posted from a datalink, you should be able to code the textbox.text
to display blank if isdbnull. So I don't see the issue, frankly.

HTH,

Bernie

"Woody Splawn" <No**********@jts.bz> wrote in message
news:%2***************@TK2MSFTNGP14.phx.gbl...
Thank you for your input.

Did you see the part in my message regarding nulls? How do you deal with
nulls?

Nov 21 '05 #6
Well here goes another class... it take care of some regional settings, and
it will also check for clipboard events.

Best wishes.
Public Class NumericTextBox
Inherits System.Windows.Forms.TextBox

Private str_OldText As String
Private int_SelectionStart As Integer
Private int_SelectionLength As Integer

Public Sub New()
MyBase.New()
Me.str_OldText = Nothing

If
(System.Globalization.NumberFormatInfo.CurrentInfo .NegativeSign.Length <> 1)
Or (System.Globalization.NumberFormatInfo.CurrentInfo .NumberDecimalSeparator
<> 1) Then
Throw New Exception("This class can't be used with your current
regional settings.")
End If
End Sub

Public Overrides Property Text() As String
Get
Return MyBase.Text
End Get
Set(ByVal Value As String)
If Not IsNumeric(Value) Then
Beep()
MyBase.Text = ""
Else
MyBase.Text = Value
End If
End Set
End Property

Protected Overrides Sub OnKeyDown(ByVal e As
System.Windows.Forms.KeyEventArgs)
If (e.Shift) And (e.KeyCode = Keys.Insert) Then
If Clipboard.GetDataObject.GetDataPresent(DataFormats .Text) Then
If Not
IsNumeric(Clipboard.GetDataObject.GetData(DataForm ats.Text)) Then
Beep()
e.Handled = True
End If
Else
Beep()
e.Handled = True
End If
End If
End Sub

Protected Overrides Sub OnKeyPress(ByVal e As
System.Windows.Forms.KeyPressEventArgs)
If Not Char.IsControl(e.KeyChar) Then
If Not Char.IsDigit(e.KeyChar) Then
Select Case e.KeyChar
Case
System.Globalization.NumberFormatInfo.CurrentInfo. NumberDecimalSeparator
If InStr(Me.Text,
System.Globalization.NumberFormatInfo.CurrentInfo. NumberDecimalSeparator)
Then
Beep()
e.Handled = True
ElseIf Me.SelectionStart = 0 Then
If Me.SelectionLength = Me.Text.Length Then
Me.Text = "0" &
System.Globalization.NumberFormatInfo.CurrentInfo. NumberDecimalSeparator
Me.SelectionStart = 2
e.Handled = True
Else
Me.Text = "0" &
System.Globalization.NumberFormatInfo.CurrentInfo. NumberDecimalSeparator &
Me.Text.Substring(Me.SelectionLength)
Me.SelectionStart = 2
e.Handled = True
End If
End If
Case
System.Globalization.NumberFormatInfo.CurrentInfo. NegativeSign
If Me.SelectionStart = 0 Then
If Me.Text.Length > 0 Then
If Me.Text.Substring(0, 1) =
System.Globalization.NumberFormatInfo.CurrentInfo. NegativeSign Then
Beep()
e.Handled = True
End If
End If
Else
Beep()
e.Handled = True
End If
Case Else
Beep()
e.Handled = True
End Select
End If
End If
End Sub

Protected Overrides Sub OnMouseDown(ByVal e As
System.Windows.Forms.MouseEventArgs)
Me.str_OldText = Me.Text
Me.int_SelectionStart = Me.SelectionStart
Me.int_SelectionLength = Me.SelectionLength
End Sub

Protected Overrides Sub OnMouseUp(ByVal mevent As
System.Windows.Forms.MouseEventArgs)
If Not IsNumeric(Me.Text) Then
Me.Text = Me.str_OldText
Me.SelectionStart = Me.int_SelectionStart
Me.SelectionLength = Me.int_SelectionLength
End If

Me.str_OldText = Nothing
Me.Refresh()
End Sub

Protected Overrides Sub OnPaint(ByVal e As
System.Windows.Forms.PaintEventArgs)
If Me.str_OldText Is Nothing Then
MyBase.OnPaint(e)
End If
End Sub
End Class
Nov 21 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: T. Wintershoven | last post by:
Hello, I have a few textboxes placed on a SSTab control devided over 3 tabs Is there a way to clear all textboxes in one time in stead of one by one. Someone told me "Use for each item...
0
by: blurz | last post by:
Hi, would like to know how to go about validating a a group of textboxes. For example, I have 3 textboxes that will hold a user's different contact numbers. How do I ensure that at least one of...
13
by: Martin Ho | last post by:
I know this must be trivial for many of you. But I am playing with this and can't figure it out. I have a form, on that form is one panel which has 3 textboxes, when I run my program and...
3
by: Sathyaish | last post by:
What's with these newline characters and textboxes. I've noticed this in .NET and it wasn't prevelant in Visual Studio 6.0. Unless you have a richtext box, which displays the text representation...
2
by: noosaj | last post by:
Hi, I'm designing a basic sales tax calculator. I'm in a bind here. I am trying to figure out how to prevent the user from inputting anything other than numbers in the textboxes. Obviously,...
0
by: Elmo Watson | last post by:
I've got quite a few textboxes on my page, along with labels, within a table. In these textboxes, I use the onTextChanged event (with autopostback=true) to calculate some formulas, and put the...
1
by: Peirski | last post by:
Hi all Situation: a form containing 10 textboxes with a button to validate those textboxes The user will have to complete these textboxes with numbers from 1 to 10 and each number can only...
1
by: Greg Smith | last post by:
Is there a way to mask input for a TextBox? I would like to have TextBoxes that accept only: - numbers - currency Any help is greatly appreciated.
7
by: Sriku | last post by:
I have created ten textboxes and name them as "txt1,txt2,..txt10" (i.e just changing suffix by consecutive number). Now I have to validate these ten textboxes on Submit event (Say..No textbox...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...
3
SueHopson
by: SueHopson | last post by:
Hi All, I'm trying to create a single code (run off a button that calls the Private Sub) for our parts list report that will allow the user to filter by either/both PartVendor and PartType. On...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.