detect enter button on VB2005

muupeetz

Member
Joined
Apr 30, 2009
Messages
5
Programming Experience
1-3
hello friends,

i've made some console application to display ascii code but i got an exception when try it..

VB.NET:
sub main()
   Console.WriteLine("enter any character : ")
        Dim chr As String = Console.ReadLine
        Console.WriteLine("ASCII COde from character {0} = {1}", chr, Asc(chr))
end sub()

the result :

enter any character :
a
ASCII COde from character a = 97
Press any key to continue . . .

but when i tried pressing enter button without entering any character it shows error :

Unhandled Exception: System.ArgumentException: Length of argument 'String' must
be greater than zero.
at Microsoft.VisualBasic.Strings.Asc(String String)
at StringASCII.Module1.Main() in D:\StringASCII\Module1.vb:line 36
Press any key to continue . . .

my question is :

1. how i can trap / detect enter button without entering any character so it won't show any error

thank u so much for any replies :)
 
Don't use chr as a variable name. It's a reserved word. Use ch instead. Also, control characters won't work with Console.ReadLine. You need to use ReadKey instead. This works:

Sub Main()
Console.Write("Enter any character: ")
Dim ch As String = Console.ReadKey().KeyChar
Console.WriteLine()
Console.WriteLine("ASCII Code from character {0} = {1}", ch, Asc(ch))
Console.ReadLine()
End Sub
 
Last edited:
You could also try this :
VB.NET:
Dim ch As String=String.Empty
While ch=String.Empty
    Console.Writeline("Enter any character")
    ch=Console.ReadKey
End While
'Then the rest of your code after.
 
Last edited by a moderator:
Thanks for the edit JuggaloBrotha, I usually use my mobile so formatting has been a little hard for me, but now i've got the hang of it.
 
For greater versatility, try this to repeat until user presses the Esc key:

VB.NET:
	Sub Main()
		Dim ch As String
		Console.WriteLine("Press Esc to quit.")
		Console.WriteLine()
		Do
			Console.Write("Enter any character: ")
			ch = Console.ReadKey().KeyChar
			Console.WriteLine()
			Console.WriteLine("ASCII Code from character {0} = {1}", ch, Asc(ch))
			Console.WriteLine()
		Loop Until ch = Chr(27)
		Console.ReadKey()
	End Sub
 
VB.NET:
Console.WriteLine("Type any character, press Enter to exit.")
Dim info As ConsoleKeyInfo = Console.ReadKey(True)
Do Until info.Key = ConsoleKey.Enter
    Console.WriteLine("ASCII code from character {0} = {1}", info.KeyChar, Convert.ToInt32(info.KeyChar))
    info = Console.ReadKey(True)
Loop
 
Put instructions in the title so if it scrolls down, it still shows. Skip lines between each new character. Last input for Esc shows the info and displays instruction to press any key to end:


VB.NET:
		Dim ch As String
		Console.Title = "Type any character.  Press Esc to quit. "
		Do
			ch = Console.ReadKey(True).KeyChar
			Console.WriteLine()
			Console.WriteLine("ASCII Code from character {0} = {1}", ch, Asc(ch))
			Console.WriteLine()
		Loop Until ch = Chr(27)
		Console.Write("Press any key to end...")
		Console.ReadKey()
 
Back
Top