1
我想创建一个屏幕,用户将能够用StringBuilder写下他们的名字。我遇到的问题是退格的功能。我可以删除除按下的第一个字母之外的所有字母。此外,似乎我可以按任何字符,它会被提交。退格将无法正常工作
ConsoleKeyInfo cki = new ConsoleKeyInfo();
bool enterPressed = false;
StringBuilder name = new StringBuilder();
int temp = 61;
do
{
Console.SetCursorPosition(temp, 14);
cki = Console.ReadKey(true);
if (cki.Key == ConsoleKey.Enter && name.Length > 0 && name.Length < 12)
{
enterPressed = true;
Console.SetCursorPosition(61, 18);
Console.Write(name);
}
else if ("qwertyuiopasdfghjklzxcvbnm".Contains(cki.KeyChar) && name.Length < 12)
{
name.Append(cki.KeyChar);
Console.Write(cki.KeyChar);
temp += 1;
}
else if(cki.Key == ConsoleKey.Backspace && name.Length > 0)
{
name.Remove(name.Length-1, 1);
Console.Write("\b \b");
}
} while (name.Length > 0 && !enterPressed);
您是否知道Console.ReadLine()?已经做到了你想要的我想。 –