Numeric values in textbox.

Q

Qwert

Hello,

how do you create a textbox control that can only hold numeric values?

Thanks,

Qwert.
 
C

Chris, Master of All Things Insignificant

Simple google search turns up this as the first listing:
http://www.a1vbcode.com/snippet.asp?ID=2408

Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
e.Handled = TrapKey(Asc(e.KeyChar))
End Sub

Private Function TrapKey(ByVal KCode As String) As Boolean
If (KCode >= 48 And KCode <= 57) Or KCode = 8 Then
TrapKey = False
Else
TrapKey = True
End If
End Function

Chris
 
M

Mohamoss

Hi Qwert
Simply you can create a class that inherit the windows form textbox
control . and modify the get string to accept ony numeric character
otherwise reaise exception , show error message , or whatever you want your
logic to be . then use that class into your form . check also this on how
to detect a number entered http://www.a1vbcode.com/snippet.asp?ID=2408
Hope this helps
Mohamed M .Mahfouz
Developer Support Engineer
ITWorx on behalf of Microsoft EMEA GTSC
 
G

Guest

This code is what I wrote a few years ago. It accepts numbers, in 1 decimal
point & 1 minus sign too:

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
Case 45
If InStr(Me.Text, "-") <> 0 Then
KeyAscii = 0
End If
If Me.SelectionStart <> 0 Then
KeyAscii = 0
End If
Case 46
If InStr(Me.Text, ".") <> 0 Then
KeyAscii = 0
End If
Case Else
KeyAscii = 0
End Select

If KeyAscii = 0 Then
e.Handled = True
Else
e.Handled = False
End If
End Sub

I hope this helps
 
H

Herfried K. Wagner [MVP]

Crouchie1998 said:
This code is what I wrote a few years ago. It accepts numbers, in 1
decimal
point & 1 minus sign too:

.... but it won't prevent the user from pasting text from the clipboard :).
 
Q

Qwert

Thank you all.

I turned into this:

Private Sub TextBoxNumericOnly(ByRef strTextBox As TextBox, ByRef e As
System.Windows.Forms.KeyPressEventArgs, Optional ByVal blnAllowNegative As
Boolean = False, Optional ByVal blnAllowDecimal As Boolean = False)

Try
With strTextBox
e.Handled = True
Select Case Convert.ToInt32(e.KeyChar)
Case 48 To 57, 8, 13 ' 0 to 9, backspace, enter, delete.
e.Handled = False
Case 45 '-'.
REM You may only add 1 '-' char and only if it it
the first.
If blnAllowNegative Then
If (.Text.Length - .SelectedText.Length) = 0
Then
If .Text.LastIndexOf("-"c) = -1 Then
e.Handled = False
End If
End If
End If
Case 46 '.'.
REM You may only add 1 '.' char.
If blnAllowDecimal Then
If .Text.LastIndexOf("."c) = -1 Then
e.Handled = False
End If
End If
End Select
End With
Catch Ex As Exception
ExToMsg(Ex, mstrMessage)
End Try

End Sub

but you can still press CTRL+V or use rightclick menu paste or when you
select all text you can't press the '-' or '.' when the selected text
contains one of them even though it would be replaced.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top