Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
How to force numeric entry?
Message
General information
Forum:
ASP.NET
Category:
Forms
Miscellaneous
Thread ID:
00746296
Message ID:
00746363
Views:
18
I would recommend subclassing a textbox to allow you to reuse it. Then you need to put code in the KeyPress event to trap the keystrokes. Here is the KeyPress sample code:
Private Sub NumericTextBox_KeyPress(ByVal sender As Object, _
      ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles MyBase.KeyPress
  Dim KeyAscii As Integer
  KeyAscii = Asc(e.KeyChar)

  Select Case KeyAscii

    Case 48 To 57, 8, 13      ' these are the digits 0-9, backspace, 
      ' and carriage return
      ' we're OK on these, don't do anything

    Case 45                   ' minus sign

      ' The number can only have one minus sign, so 
      ' if we already have one, throw this one away
      If InStr(Me.Text, "-") <> 0 Then
        KeyAscii = 0
      End If

      ' if the insertion point is not sitting at zero
      ' (which is the beginning of the field), throw away the minus
      ' sign (because it's not valid except in first position)
      If Me.SelectionStart <> 0 Then
        KeyAscii = 0
      End If

    Case 46                   ' this is a period (decimal point)

      ' if we already have a period, throw it away
      If InStr(Me.Text, ".") <> 0 Then
        KeyAscii = 0
      End If

    Case Else
      ' provide no handling for the other keys
      KeyAscii = 0

  End Select

  ' If we want to throw the keystroke away, then set the event
  ' as already handled. Otherwise, let the keystroke be handled normally.
  If KeyAscii = 0 Then
    e.Handled = True
  Else
    e.Handled = False
  End If

End Sub
>Hi all,
>
>How to force a form textbox to accept numaric only, while the textbox is using a DataBindings?
>
>Thanks for the help
-----------------------------------------

Cathi Gero, CPA
Prenia Software & Consulting Services
Microsoft C# / .NET MVP
Mere Mortals for .NET MVP
cgero@prenia.com
www.prenia.com
Weblog: blogs.prenia.com/cathi
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform