[VB] How to do this simple shit?

Status
Not open for further replies.

xAlphaPlayerx

Senior
Original poster
senior
member
Joined
Jul 27, 2011
Posts
1,206
Reaction score
147
Points
46
Location
Karkand
How can i set a TextBox to only can write numbers?

I know, nub question, that's what I'm :D

Thanks!
 

Markedo

Registered
Joined
Sep 1, 2011
Posts
1
Reaction score
1
Points
0
How to check for numerical input in VB.net 2010

Hello,

Code:
Private Sub TextBox1_TextChanged(sender As System.Object, e As System.EventArgs) Handles TextBox1.TextChanged

        If Not IsNumeric(TextBox1.Text) Then
            TextBox1.Text = ""
        End If

End Sub

When the user writes non-numerical input in the textbox it will reset to "" (nothing)
EDIT: Maybe adding a messagebox to show the user what hes doing wrong.
Good luck :-)
 
  • Like
Reactions: xAlphaPlayerx
K

KamaluNg

Guest
Might be much, but heres everything explained: http://msdn.microsoft.com/en-us/library/ms229644.aspx
 

joomador

Registered
Joined
Aug 30, 2010
Posts
7
Reaction score
0
Points
0
And display it on a message box


If Not IsNumeric(TextBox1.Text) Then
MessageBox.Show("You need to use Numbers")
Exit Sub
End If
 

xAlphaPlayerx

Senior
Original poster
senior
member
Joined
Jul 27, 2011
Posts
1,206
Reaction score
147
Points
46
Location
Karkand
And display it on a message box


If Not IsNumeric(TextBox1.Text) Then
MessageBox.Show("You need to use Numbers")
Exit Sub
End If
Yea, I did before. But i have a little problem: if the user have time to write 2 letters, two msgBoxes will appear. How can i make only one appear?

Thanks anyway! :)
 

Darkness

Veteran
veteran
old school
senior
Life Saver
coldschool
member
Joined
Jul 17, 2009
Posts
3,361
Reaction score
2,442
Points
180
Location
Toronto, Canada
Code:
Dim once as Boolean = true
If Not IsNumeric(TextBox1.Text) Then
If once = true Then
MessageBox.Show("You need to use Numbers")
Once = false
End If
Exit Sub
End If

For the messagebox
 
  • Like
Reactions: xAlphaPlayerx

Nick55

Member
member
Joined
Apr 11, 2011
Posts
6
Reaction score
0
Points
0
It is just a suggestion, but can't you use a MaskedTextBox instead of a regular textbox?With a masked textbox ,you can restrict the input to only numbers, no extra coding is necessary...Just an idea...
 
Status
Not open for further replies.