Exclamation[Help]Have TextBoxes only have Numbers[Solved]

Posts 1–15 of 46 · Page 1 of 4
[Help]Have TextBoxes only have Numbers[Solved]
I am trying to make a program that needs Nubmers Only in a TextBox, but i don't know how to do it. If i had to do the TextBox1.Text.Contains("a" and "b"...) it would take Tooo long.

Thanks for your help.
- KeyPressEventArgs (e) -> Use it to block the keypress if the keycode isn't a number (according to ascii codes, don't forget backspace and delete)
- Key ASCII code



Event is either keypress or keydown.

Try it yourself first. If you can't make it. Then i will help you. Just try it. (it should be really simple.. seriously)
Use a NumericUpDown?
Quote Originally Posted by Blubb1337 View Post
Use a NumericUpDown?
meh phonenumbers, zip codes.. etc.. would be really annoying to use these
Quote Originally Posted by o0OpurezO0o View Post
I am trying to make a program that needs Nubmers Only in a TextBox, but i don't know how to do it. If i had to do the TextBox1.Text.Contains("a" and "b"...) it would take Tooo long.

Thanks for your help.
Code:
If not IsNotNumeric( txtYourTextbox.text) then
MessageBox.show("Only numbers are allowed!") 
Else
'Do what you want...
End If
I think this coud help you alot, just paste this somewhere at a button click or when user types it in your textbox

Idk if you meant this but i hope it'll help you =)
Quote Originally Posted by CptnDutch View Post


Code:
If not IsNotNumeric( txtYourTextbox.text) then
MessageBox.show("Only numbers are allowed!") 
Else
'Do what you want...
End If
I think this coud help you alot, just paste this somewhere at a button click or when user types it in your textbox

Idk if you meant this but i hope it'll help you =)
that would not block it while writing tho..
Quote Originally Posted by 'Bruno View Post
that would not block it while writing tho..
* txtYourTextbox.clear ?? then it will delete the text everytime it isn't numeric =)
Quote Originally Posted by CptnDutch View Post


* txtYourTextbox.clear ?? then it will delete the text everytime it isn't numeric =)
on a button event... I mean.. He is writing something big.
After clicking the button:

"Oh wait.. you can't write letters.. or number 1.. or.. w/e.." ./facepalm

Blocking it while writing would be ok. Unless you use some kind of method to block on keypress..
I would advice just to block the keypress and everything would be way more simple.
Quote Originally Posted by o0OpurezO0o View Post
lol i was coding the whole time and then i get 7 replys lol

and no the code:

Code:
If not IsNotNumeric( txtYourTextbox.text) then
MessageBox.show("Only numbers are allowed!") 
Else
'Do what you want...
End If
doesnt work.
You need to change the txtYourTextbox to your named textbox you know right?
When you put that code into let us say a Button press, it should give a message when you wrote lettres in that textbox ! Else put it in the txtYourTextbox.textchanged sub

Change txtYourTextbox -> the name you gave to your textbox


Quote Originally Posted by 'Bruno View Post
on a button event... I mean.. He is writing something big.
After clicking the button:

"Oh wait.. you can't write letters.. or number 1.. or.. w/e.." ./facepalm

Blocking it while writing would be ok. Unless you use some kind of method to block on keypress..
I would advice just to block the keypress and everything would be way more simple.
He can try to use the event textbox.textchanged (if i remember it right)
Then it will block it when the user writes a lettre (A-Z) in it, and it wont when you type a number (0-9)
lol i was coding the whole time and then i get 7 replys lol

and no the code:

Code:
If not IsNotNumeric( txtYourTextbox.text) then
MessageBox.show("Only numbers are allowed!") 
Else
'Do what you want...
End If
doesnt work.
oh god... you people are lazy and don't really feel like learning. You guys get scared with some theory on how to do it, instead of just coder ready to use. There you go..

[highlight=C#]
if (e.KeyChar >= 48 && e.KeyChar <= 57)
e.Handled = false;
else if(e.KeyChar == 8)
e.Handled = false;
else
e.Handled = true;
[/highlight]

That's C#, you should be able to write such simple things. (Same .NET so..)

Sorry about C#, it's faster for me.

This will just let you press 0-9 and backspace to delete. Use it on Textbox Keypress event. And try it yourself next time instead of just ignoring what people tells you.
Quote Originally Posted by 'Bruno View Post
oh god... you people are lazy and don't really feel like learning. You guys get scared with some theory on how to do it, instead of just coder ready to use. There you go..

[highlight=C#]
if (e.KeyChar >= 48 && e.KeyChar <= 57)
e.Handled = false;
else if(e.KeyChar == 8)
e.Handled = false;
else
e.Handled = true;
[/highlight]

That's C#, you should be able to write such simple things. (Same .NET so..)

Sorry about C#, it's faster for me.

This will just let you press 0-9 and backspace to delete. Use it on Textbox Keypress event.
My method is much easier and is written in vb.net so he doesn't needs to convert it from C# to .net

Just let us see the code oOo..oOo
Quote Originally Posted by CptnDutch View Post


My method is much easier and is written in vb.net so he doesn't needs to convert it from C# to .net

Just let us see the code oOo..oOo
easier? how is it easier? and it is way worse in my opinion.
btnStart is the button
txtNumbersOnly is the textbox

For a button click:
Code:
    Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
        If Not IsNumeric(txtNumbersOnly.Text) Then
            MessageBox.Show("Only numbers are allowed!")
        Else
            'Let it proceed, what you want to do it!
        End If
    End Sub
For when the text ïn the textbox changed: This can be laggy cause it'll keep giving errors
Code:
    Private Sub txtNumbersOnly_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtNumbersOnly.TextChanged
        If Not IsNumeric(txtNumbersOnly.Text) Then
            MessageBox.Show("Only numbers are allowed!")
        Else
            'Let it proceed, what you want to do it!
        End If
    End Sub
Just change the textbox and the button name to wich you named it

Hopes this helpes you ^^


Quote Originally Posted by 'Bruno View Post
easier? how is it easier? and it is way worse in my opinion.
He just needs to adapt this code to his project, yours is much complicated for a 'new' one in the programming world, this is understandable and easy if you know the basic structures!
Quote Originally Posted by CptnDutch View Post
btnStart is the button
txtNumbersOnly is the textbox

For a button click:
Code:
    Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
        If Not IsNumeric(txtNumbersOnly.Text) Then
            MessageBox.Show("Only numbers are allowed!")
        Else
            'Let it proceed, what you want to do it!
        End If
    End Sub
For when the text ïn the textbox changed: This can be laggy cause it'll keep giving errors
Code:
    Private Sub txtNumbersOnly_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtNumbersOnly.TextChanged
        If Not IsNumeric(txtNumbersOnly.Text) Then
            MessageBox.Show("Only numbers are allowed!")
        Else
            'Let it proceed, what you want to do it!
        End If
    End Sub
Just change the textbox and the button name to wich you named it

Hopes this helpes you ^^




He just needs to adapt this code to his project, yours is much complicated for a 'new' one in the programming world, this is understandable and easy if you know the basic structures!
if you say so <_<

at least instead of using a messagebox to tell him that he can't insert it and still inserting it (on your code)
Just block the input. Like i did..
Posts 1–15 of 46 · Page 1 of 4
This thread is closed for replies.

Tags for this Thread

None

Talk with us