Check a textbox.text is number

  • Thread starter Thread starter Guest
  • Start date Start date
Hi Joe,

The anser on your question depends on the programming language you are
using.

By instance VBNet has some very simple functions for it

In my opinion is it better to ask this question in one of the language
groups.

Microsoft.public.dotnet.languages.Csharp
or
Microsoft.public.dotnet.languages.VB
or
Microsoft.public.dotnet.languages.VC
or
for J# there is one with a different signature

I hope this helps?

Cor
 
Hi Joe,

When it is in a validating event just use

If Not IsNumeric(me.textbox1.text) then

When you want it something more advanced you can use the code below I once
made as a sample.

I hope this helps?

(Take next time the language VB newsgroups for questions like this,
especially in casting and testing of values has VB a lot more methods than
C#, so that is a more properiate newsgroup for that)

Cor

\\\
Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Me.TextBox1.MaxLength = 10
End Sub
Private Sub textbox1_KeyUp(ByVal sender As Object, _
ByVal e As System.Windows.Forms.KeyEventArgs) Handles textbox1.KeyUp
If e.KeyValue <> 8 Then
If Not IsNumeric(TextBox1.Text) Then
If TextBox1.Text.Length > 0 Then
MessageBox.Show("Only numeric is allowed")

TextBox1.SelectionStart = TextBox1.Text.Length - 1
TextBox1.SelectionLength = 1
End If

End If
End If
End Sub
Private Sub TextBox1_Validating(ByVal sender _
As Object, ByVal e As System.ComponentModel.CancelEventArgs) _
Handles TextBox1.Validating
If TextBox1.Text.Length > 0 Then
If Not IsNumeric(TextBox1.Text) Then
MessageBox.Show("There was an error pasting")
TextBox1.Focus()
e.Cancel = True
End If
End If
End Sub
///
 
Back
Top