Hi, vitaminchik
not through Enter, but through an arrow
I'm afraid Console.ReadLine can't achieve what you want.
If the standard input device is the keyboard, the ReadLine method blocks until the user presses the Enter key.
The ReadLine method executes synchronously. That is, it blocks until a line is read or the <kbd>Ctrl+Z</kbd> keyboard combination (followed by <kbd>Enter</kbd> on Windows), is pressed.
Update:
It is a possibility. Use readkey instead of Console.ReadLine();
to read individual characters and then concatenate them.
Below is a quick test,and the code is not optimized.
public static void Print()
{
origRow = Console.CursorTop;
origCol = Console.CursorLeft;
ConsoleKeyInfo ki;
PrintMenu();
WriteAt(5, selectedValue + 1);
String temp = string.Empty;
do
{
ki = Console.ReadKey();
switch (ki.Key)
{
case ConsoleKey.UpArrow:
{
variables[selectedValue] = temp;
introducedVariables[selectedValue] = variables[selectedValue];
if (selectedValue > 0)
selectedValue--;
}
temp = string.Empty;
break;
case ConsoleKey.DownArrow:
variables[selectedValue] = temp;
introducedVariables[selectedValue] = variables[selectedValue];
if (selectedValue < strings.Length - 1)
{
selectedValue++;
}
temp = string.Empty;
break;
case ConsoleKey.Enter:
variables[selectedValue] = temp;
introducedVariables[selectedValue] = variables[selectedValue];
break;
default:
{
temp += ki.KeyChar.ToString();
}
continue;
}
Console.Clear();
PrintMenu();
WriteAt(5, selectedValue + 1);
}
while (ki.Key != ConsoleKey.Enter) ;
string line = new string('-', 50);
Console.WriteLine(line);
}
protected static void WriteAt(int x, int y)
{
try
{
Console.SetCursorPosition(origCol + x, origRow + y);
}
catch (ArgumentOutOfRangeException e)
{
Console.Clear();
Console.WriteLine(e.Message);
}
}
Best regards,
Minxin Yu
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.