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.