13 Replies - 57437 Views - Last Post: 16 October 2008 - 09:54 AM Rate Topic: -----

#1 Mangore  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 177
  • Joined: 11-October 08

only numbers in textbox

Posted 15 October 2008 - 05:01 PM

I use this code that I got it from web is to allowed to write in textbox only numbers but it show me a problem as it show in the pic. I'm using vb2005

 If Asc(e.KeyChar) <> 13 AndAlso Asc(e.KeyChar) <> 8 _
	   AndAlso Not IsNumeric(e.KeyChar) Then
			MessageBox.Show("Only Numbers")
			e.Handled = True
		End If

Attached image(s)

  • Attached Image

This post has been edited by Mangore: 15 October 2008 - 05:02 PM

Is This A Good Question/Topic? 0
  • +

Replies To: only numbers in textbox

#2 jacobjordan  Icon User is offline

  • class Me : Perfection
  • member icon

Reputation: 113
  • View blog
  • Posts: 1,499
  • Joined: 11-June 08

Re: only numbers in textbox

Posted 15 October 2008 - 05:10 PM

You were supposed to put that code in a KeyDown event, not a TextChanged Event. Create a KeyDown event for your text box, and put that same code in it. Like this:
Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
    If Asc(e.KeyChar) <> 13 AndAlso Asc(e.KeyChar) <> 8 _
       AndAlso Not IsNumeric(e.KeyChar) Then
            MessageBox.Show("Only Numbers")
            e.Handled = True
    End If
End Sub


Was This Post Helpful? 0
  • +
  • -

#3 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: only numbers in textbox

Posted 15 October 2008 - 06:00 PM

It will also work in the KeyPress event. You can find an example here.
Was This Post Helpful? 1

#4 Mangore  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 177
  • Joined: 11-October 08

Re: only numbers in textbox

Posted 15 October 2008 - 06:03 PM

View Postjacobjordan, on 15 Oct, 2008 - 05:10 PM, said:

You were supposed to put that code in a KeyDown event, not a TextChanged Event. Create a KeyDown event for your text box, and put that same code in it. Like this:
Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
    If Asc(e.KeyChar) <> 13 AndAlso Asc(e.KeyChar) <> 8 _
       AndAlso Not IsNumeric(e.KeyChar) Then
            MessageBox.Show("Only Numbers")
            e.Handled = True
    End If
End Sub



Same problem.

Attached image(s)

  • Attached Image

Was This Post Helpful? 0
  • +
  • -

#5 Mangore  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 177
  • Joined: 11-October 08

Re: only numbers in textbox

Posted 15 October 2008 - 06:24 PM

View PostMangore, on 15 Oct, 2008 - 06:03 PM, said:

View Postjacobjordan, on 15 Oct, 2008 - 05:10 PM, said:

You were supposed to put that code in a KeyDown event, not a TextChanged Event. Create a KeyDown event for your text box, and put that same code in it. Like this:
Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
    If Asc(e.KeyChar) <> 13 AndAlso Asc(e.KeyChar) <> 8 _
       AndAlso Not IsNumeric(e.KeyChar) Then
            MessageBox.Show("Only Numbers")
            e.Handled = True
    End If
End Sub



Same problem.


It succeed with me in Keypress
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
        If Asc(e.KeyChar) <> 13 AndAlso Asc(e.KeyChar) <> 8 _
        AndAlso Not IsNumeric(e.KeyChar) Then
            MessageBox.Show("Only Numbers")
            e.Handled = True
        End If

May some one eaxplain it
Was This Post Helpful? 1
  • +
  • -

#6 jacobjordan  Icon User is offline

  • class Me : Perfection
  • member icon

Reputation: 113
  • View blog
  • Posts: 1,499
  • Joined: 11-June 08

Re: only numbers in textbox

Posted 15 October 2008 - 07:04 PM

I screwed up. It will not work in KeyDown, as the KeyDown event has a different signature than the KeyPress event. In other words, the event args for the KeyPress event is different than that for the KeyDown event. However, in the KeyPress event, i do not believe there is any way to supress the key press, like you can do in the KeyDown event with the e.SupressKeyPress property.

This post has been edited by jacobjordan: 15 October 2008 - 07:04 PM

Was This Post Helpful? 0
  • +
  • -

#7 Mangore  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 177
  • Joined: 11-October 08

Re: only numbers in textbox

Posted 15 October 2008 - 07:30 PM

What does mean "e" and "KeyChar" and Asc(e.KeyChar) <> 13 AndAlso Asc(e.KeyChar) <> 8
Was This Post Helpful? 0
  • +
  • -

#8 jacobjordan  Icon User is offline

  • class Me : Perfection
  • member icon

Reputation: 113
  • View blog
  • Posts: 1,499
  • Joined: 11-June 08

Re: only numbers in textbox

Posted 15 October 2008 - 07:55 PM

'e' is a parameter passed to the KeyPress function. It contains information about the keypress. The "KeyChar" property returns a char of the key that was pressed, so if you pressed the a key, it would return 'a'. The Asc() function is a function that will take a char, and return that char's ASCII code. The "Asc(e.KeyChar) <> 13" checks to make sure the char entered was not a Carriage return, which i really don't see why it would need that. The "Asc(e.KeyChar) <> 8" is checking to see that the backspace key was not pressed, as that is obviously not numeric, but accepted. The IsNumeric() function simply sees if the char you passed it is in a valid numeric format. Put that all together in the form of Asc(e.KeyChar) <> 13 AndAlso Asc(e.KeyChar) <> 8 AndAlso Not IsNumeric(e.KeyChar), and that will evaluate to true if the e.KeyChar is not numeric, was not a carriage return, and not a backspace, thus is is not an accepted key.
Was This Post Helpful? 1
  • +
  • -

#9 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1641
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: only numbers in textbox

Posted 15 October 2008 - 08:37 PM

If you look at the signature of your method it has ByVal e As EventArgs when it should be ByVal e As KeyEventArgs. KeyChar is a member of KeyEventArgs, not EventArgs
Was This Post Helpful? 0
  • +
  • -

#10 jacobjordan  Icon User is offline

  • class Me : Perfection
  • member icon

Reputation: 113
  • View blog
  • Posts: 1,499
  • Joined: 11-June 08

Re: only numbers in textbox

Posted 15 October 2008 - 08:56 PM

View PostPsychoCoder, on 15 Oct, 2008 - 10:37 PM, said:

If you look at the signature of your method it has ByVal e As EventArgs when it should be ByVal e As KeyEventArgs. KeyChar is a member of KeyEventArgs, not EventArgs
Actually Psycho, your wrong. KeyChar is a member of KeyPressEventArgs, not KeyEventArgs. I made the same mistake in my first post.
Was This Post Helpful? 0
  • +
  • -

#11 Mangore  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 177
  • Joined: 11-October 08

Re: only numbers in textbox

Posted 15 October 2008 - 08:58 PM

Thank you all for help!. It is a good infromation for me
Was This Post Helpful? 0
  • +
  • -

#12 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1641
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: only numbers in textbox

Posted 15 October 2008 - 10:43 PM

You are right, it's KeyCode that's with KeyEventArgs, which is what I normally use
Was This Post Helpful? 0
  • +
  • -

#13 dbasnett  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 109
  • View blog
  • Posts: 603
  • Joined: 01-October 08

Re: only numbers in textbox

Posted 16 October 2008 - 07:44 AM

IMHO - I hate that a message box pops up while I am trying to input information.

	'list of valid characters allowed in textbox
	'note if checking numbers, and the number is integer remove "."
	'if checking numbers, and the number has decimals then further checking is required
	Dim validCH() As Char = New Char() {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", ".", Convert.ToChar(vbCr)}
	Private Sub TextBoxNumOnly_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBoxNumOnly.KeyPress
		Debug.WriteLine(Convert.ToByte(e.KeyChar))
		If Array.IndexOf(validCH, e.KeyChar) <> -1 Then
			'character was good code here
			Exit Sub
		End If
		'character was bad code here
		e.Handled = True
	End Sub


Was This Post Helpful? 0
  • +
  • -

#14 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2263
  • View blog
  • Posts: 9,467
  • Joined: 29-May 08

Re: only numbers in textbox

Posted 16 October 2008 - 09:54 AM

I think you need something like this, suppress everything except those allowed and in the correct format.
 Private Sub InputBox_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles InputBox.KeyDown
Select Case e.KeyCode
 Case Keys.D0, Keys.D1, Keys.D2, Keys.D3, Keys.D4, Keys.D5, Keys.D6, Keys.D7, Keys.D8, Keys.D9
   e.SuppressKeyPress = False

 Case Keys.Decimal, Keys.OemPeriod
  If Me.InputBox.Text.Contains(".") Then e.SuppressKeyPress = True
Case Keys.Subtract, Keys.OemMinus
If Me.InputBox.Text.StartsWith("-") Then e.SuppressKeyPress = True
 Case Keys.Delete, Keys.Back, Keys.Enter, Keys.Return
  e.SuppressKeyPress = False
 Case Else
 e.SuppressKeyPress = True
 End Select
 End Sub


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1