❗ [Help]Have TextBoxes only have Numbers[Solved]
Okay you want one line?
But lines don't really count for everything all the time.
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.
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,
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.
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.
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]
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!
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......
[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.
This thread is closed for replies.
