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

Posts 31–45 of 46 · Page 3 of 4
Quote Originally Posted by willrulz188 View Post
Dim num as integer

Try
num = textbox1.text
Kid, now you're asking for it ! -.-
Quote Originally Posted by Blubb1337 View Post
That's not what he wants.

Enter a123 in the textbox and the result will be 0 or it will just crash...
no because every time a key is released then it just checks if the last letter is a letter or number and if it is a letter then it just textbox.remove's it.
Quote Originally Posted by mnpeepno2 View Post
1 line vs >1line.
Okay you want one line?

Code:
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
    If Not IsNumeric(e.KeyChar) AndAlso Not Asc(e.KeyChar) = Keys.Back AndAlso Not Asc(e.KeyChar) = Keys.Delete Then e.Handled = True
End Sub 


But lines don't really count for everything all the time.
Quote Originally Posted by Jason View Post


Okay you want one line?

Code:
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
    If Not IsNumeric(e.KeyChar) AndAlso Not Asc(e.KeyChar) = Keys.Back AndAlso Not Asc(e.KeyChar) = Keys.Delete Then e.Handled = True
End Sub 


But lines don't really count for everything all the time.
True, someone could try to squeeze their whole program into one line. Although it saves spaces, it's not readable.
Quote Originally Posted by master131 View Post
True, someone could try to squeeze their whole program into one line. Although it saves spaces, it's not readable.
In this case 1 line isn't too hard to understand as it's pretty much just plain english the whole way along. However, it's when people start shoving "If"s with multiple methods into one line that shit starts getting REAL ugleh.
Ok, This is deff. Solved.
None of those work!! God. I use Jason's, i get "** is not a member of Args.ChangeEvent", i tried the other one, it got the same thing. Its all errors and its bit annoying. Nevermind though, i stopped. It was getting too hard, plus my DotNetBar is being a fucker again,
Quote Originally Posted by o0OpurezO0o View Post
None of those work!! God. I use Jason's, i get "** is not a member of Args.ChangeEvent", i tried the other one, it got the same thing. Its all errors and its bit annoying. Nevermind though, i stopped. It was getting too hard, plus my DotNetBar is being a fucker again,
I am looking forward to view a post of you that actually makes sense.
i'll never release my typing on here
Just create a string of allowed values, in this case, numerical values.
If you need to add a value to the string, then do it.


[highlight=vb]
' In the textbox keypress event
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress

Dim allow As String = "0123456789"

If allow.IndexOf(e.KeyChar) = -1 Then
e.Handled = True
End If

End Sub
[/highlight]

You can add any value to the string, so let's say it's going to be used for money then

[highlight=vb]
Dim allow As String = "$.0123456789"
[/highlight]

If you are going to use it for a range of things, then

[highlight=vb]
Dim allow As String = "0123456789()*&^%$#@!{}[]?><,./:"';"
[/highlight]

If the other codes (provided by other members) don't work, try setting

[highlight=vb]
Me.KeyPreview = True
[/highlight]

this will allow the form to check keystrokes before the controls do

An example of this would be Brinuz' example

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

[highlight=vb]
If (e.KeyChar < "0" OrElse e.KeyChar > "9") _
AndAlso e.KeyChar <> ControlChars.Back AndAlso e.KeyChar <> "." Then
e.Handled = True
End If
[/highlight]


This may not work unless you set KeyPreview to true, because the control will read the keystrokes before the form does.

In form1 load event

[highlight=vb]
Me.KeyPreview = True
[/highlight]

tested and it works without keypreview, however, just know that there are cases you may need to do it

You get the idea.
Quote Originally Posted by NextGen1 View Post
Just create a string of allowed values, in this case, numerical values.
If you need to add a value to the string, then do it.


[highlight=vb]
' In the textbox keypress event
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress

Dim allow As String = "0123456789"

If allow.IndexOf(e.KeyChar) = -1 Then
e.Handled = True
End If

End Sub
[/highlight]

This will also disallow backspace/space.
yeah, My second example allows backspace however.

[highlight=vb]
If (e.KeyChar < "0" OrElse e.KeyChar > "9") _
AndAlso e.KeyChar <> ControlChars.Back AndAlso e.KeyChar <> "." Then
e.Handled = True
End If
[/highlight]


If you want to allow space , just add a blank space to the string

[highlight=vb]
Dim allow As String = "0123456789 "
If allow.IndexOf(e.KeyChar) = -1 Then
e.Handled = True
End If
End Sub
[/highlight]

If you want both I tested this and it worked

[highlight=vb]
If (e.KeyChar < "0" OrElse e.KeyChar > "9") _
AndAlso e.KeyChar <> ControlChars.Back AndAlso e.KeyChar <> "." AndAlso e.KeyChar <> " " Then
e.Handled = True
End If
[/highlight]

You can add keys to the list using

[highlight=vb]
AndAlso e.KeyChar <> "Key here"
[/highlight]

[highlight=vb]Dim allow As String = "0123456789 "
If allow.IndexOf(e.KeyChar) = -1 Then
e.Handled = True
End If
End Sub [/highlight]

This will disallow backspace!
Your still missing the

[highlight=vb]
If (e.KeyChar < "0" OrElse e.KeyChar > "9") _
AndAlso e.KeyChar <> ControlChars.Back AndAlso e.KeyChar <> "." AndAlso e.KeyChar <> " " Then
e.Handled = True
End If
[/highlight]

Which does note disallow space or backspace......
I was only referring to the snippet I quoted, choob.
Posts 31–45 of 46 · Page 3 of 4
This thread is closed for replies.
Talk with us