How to handle Enter key...
-
Please go through...
class Test
{Console.WriteLine("1:Add\n2:Sub\n3:Mult:\n4:Div");
Console.WriteLine();
Console.WriteLine("Press 9 to Exit or Enter to Continue ");string s=Console.ReadLine();
....
}
My query is that when I enter the "ENTER" key, it does go ahead but it also does the same for the other keys except 9, for which it exits from the Program. Hope so I will get a solution
-
Please go through...
class Test
{Console.WriteLine("1:Add\n2:Sub\n3:Mult:\n4:Div");
Console.WriteLine();
Console.WriteLine("Press 9 to Exit or Enter to Continue ");string s=Console.ReadLine();
....
}
My query is that when I enter the "ENTER" key, it does go ahead but it also does the same for the other keys except 9, for which it exits from the Program. Hope so I will get a solution
Yes it will. I assume you have something along the lines of:
do
{
Console.WriteLine(...
...
s = Console.ReadLine();
} while (s != "9")If so, then it will loop round for any sequence of keypresses follewed by an ENTER other than the '9' key followed by ENTER. Remember that ReadLine will retrun with what has been pressed up to an ENTER - so if you only press enter, you will get and empty string. You will have to check for this and act accordingly. Sorry if this sound vague, but your code fragment doesn't give us a lot to go on!
All those who believe in psycho kinesis, raise my hand. My :badger:'s gonna unleash hell on your ass. :badger:tastic!
-
Please go through...
class Test
{Console.WriteLine("1:Add\n2:Sub\n3:Mult:\n4:Div");
Console.WriteLine();
Console.WriteLine("Press 9 to Exit or Enter to Continue ");string s=Console.ReadLine();
....
}
My query is that when I enter the "ENTER" key, it does go ahead but it also does the same for the other keys except 9, for which it exits from the Program. Hope so I will get a solution
hi, for reading a key you can use ReadKey method and a good practice for you're application is to use a switch statement like this:
Console.WriteLine("1:Add\\n2:Sub\\n3:Mult:\\n4:Div"); Console.WriteLine(); Console.WriteLine("Press 9 to Exit or Enter to Continue "); ConsoleKeyInfo cki = Console.ReadKey(); switch (cki) { case ConsoleKey.D9: Console.WriteLine("exit"); break; case ConsoleKey.Enter: Console.WriteLine("continue"); break; case ConsoleKey.D1: Console.WriteLine("Add"); break; case ConsoleKey.D2: Console.WriteLine("Sub"); break; case ConsoleKey.D3: Console.WriteLine("Mult"); break; case ConsoleKey.D4: Console.WriteLine("Div"); break; default: break; }
Anyway I don't understand why use the ENTER key to continue? have you more option than 1,2,3 and 4 ? :) Cheer's, Alex Manolescu