我正在控制台应用程序上的记事本上。当我按左,右,上,下键时,它应该移动光标。这是代码:
// For testing cursor moves
static void WriteMethod()
{
bool Stop = false;
Console.Clear();
Console.SetCursorPosition(0, 0);
foreach (string s in NoteLines){ Console.WriteLine(s); }
// Stop'll be true when esc is pressed but I didn't added it yet
while(Stop == false)
{
ConsoleKeyInfo key = Console.ReadKey();
Console.SetCursorPosition(Console.CursorLeft - 1, Console.CursorTop);
switch (key.Key)
{
case ConsoleKey.LeftArrow:
if (Console.CursorLeft > 0) Console.SetCursorPosition(Console.CursorLeft - 1, Console.CursorTop);
else Console.SetCursorPosition(0, ((Console.CursorTop > 0) ? Console.CursorTop - 1 : 0));
break;
case ConsoleKey.RightArrow:
if (Console.CursorLeft + 1 > Console.BufferWidth - 1) Console.SetCursorPosition(0, Console.CursorTop + 1);
else Console.CursorLeft += 1;
break;
case ConsoleKey.UpArrow:
if (Console.CursorTop > 0) Console.CursorTop -= 1;
break;
case ConsoleKey.DownArrow:
Console.SetCursorPosition(Console.CursorLeft, Console.CursorTop + 1);
break;
}
}
}
是的,光标移动了,但是在它离开之前会删除其上的char。我该如何解决?
相关分类