Ascii identification and decimal

techparag

New member
Joined
Jul 28, 2009
Messages
2
Programming Experience
Beginner
He all:

I am trying to convert Ascii or I can say sense ascii from keyboard. My intention is if the user enters a wrong value from keyboard the error shud flash on screen and for this I need to know the ascii value and convert the same to decimal.

Can anyone please help me on this

Regards,
Tech
 
The Asc() function will return the ASCII value of the first character of a string. Here's a sample:

VB.NET:
		Dim strinput As String
		Dim myasc As Integer
		strinput = TextBox1.Text
		myasc = Asc(strinput)
		MessageBox.Show(myasc.ToString)
 
Hi that worked well. But I am trying for something like this:

If I write a program to enter two nos and user presses an enter key without entering a no. then I want to display that this is wrong value and he/she shud enter a correct value. I am able to do the same for characters and space bars but enter being a command in VB I am not able the scan its ascii value of 13

Any guesses.

Thanks
Tech
 
I think you want to have the user enter a 1 or 2-digit number into a textbox. You should use the Textbox's Keypress event. The following code will do what you want, if I understood you right:


VB.NET:
	Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
		'Use the following line only if you need to limit number to 2 digits
		If TextBox1.Text.Length = 2 And e.KeyChar <> Chr(Keys.Back) Then e.Handled = True

		If Not (Char.IsDigit(e.KeyChar) Or e.KeyChar = Chr(Keys.Back) Or e.KeyChar = Chr(13)) Then
			e.Handled = True
		End If

		If e.KeyChar = Chr(13) Then
			If TextBox1.Text = "" Then
				MessageBox.Show("You must enter a number")
			Else
				Button1.Focus()
			End If
		End If
	End Sub
 
Back
Top