想要根据输入有条件地更改控制台前景色
Want to conditionally change Console foreground color based on input
我正在尝试使 KeyDown 语句起作用。所以我在写:
private void KeyDown(object sender, System.Windows.Forms.KeyEventArgs e);
System.Windows.Forms
不起作用 我读到它是一个 Visual Studio 代码,您必须转到 Project.json
并添加 System.Windows.Forms
作为依赖项。我不知道写什么来添加它。我在网上搜索并搜索了库存溢出。我找不到任何东西。
我不知道要输入什么来添加它。
我可能是错的,但我认为您不能将 System.Windows.Forms
程序集用于 .Net Core 项目。我的 Visual Studio 出现问题,所以我无法通过 project.json
尝试使用它。话虽如此,它也不会为你提供你想要的东西。
由于您希望通过控制台捕获用户的输入,并根据某些条件更改颜色 - 您必须自己手动执行此操作。
下面是一个完整的应用程序示例,展示了如何做到这一点。本质上,您必须评估输入控制台的每个字符并确定它是否为数字。如果它是一个数字,你必须将 cursor 移回 1 个位置,这样你就可以覆盖刚刚输入的值。在覆盖该值之前,您更改控制台前景色。
using System;
namespace ConsoleApp2
{
public class Program
{
public static void Main(string[] args)
{
// Set up an infinite loop that will allow us to forever type unless 'Q' is pressed.
while(true)
{
ConsoleKeyInfo pressedKey = Console.ReadKey();
// If Q is pressed, we quit the app by ending the loop.
if (pressedKey.Key == ConsoleKey.Q)
{
break;
}
// Handle the pressed key character.
OnKeyDown(pressedKey.KeyChar);
}
}
private static void OnKeyDown(char key)
{
int number;
// Try to parse the key into a number.
// If it fails to parse, then we abort and listen for the next key.
// It will fail if anything other than a number was entered since integers can only store whole numbers.
if (!int.TryParse(key.ToString(), out number))
{
return;
}
// If we get here, then the user entered a number.
// Apply our logic for handling numbers
ChangeColorOfPreviousCharacters(ConsoleColor.Green, key.ToString());
}
private static void ChangeColorOfPreviousCharacters(ConsoleColor color, string originalValue)
{
// Store the original foreground color of the text so we can revert back to it later.
ConsoleColor originalColor = Console.ForegroundColor;
// Move the cursor on the console to the left 1 character so we overwrite the character previously entered
// with a new character that has the updated foreground color applied.
Console.SetCursorPosition(Console.CursorLeft - 1, Console.CursorTop);
Console.ForegroundColor = color;
// Re-write the original character back out, now with the "Green" color.
Console.Write(originalValue);
// Reset the consoles foreground color back to what ever it was originally. In this case, white.
Console.ForegroundColor = originalColor;
}
}
}
控制台的输出将如下所示:
我正在尝试使 KeyDown 语句起作用。所以我在写:
private void KeyDown(object sender, System.Windows.Forms.KeyEventArgs e);
System.Windows.Forms
不起作用 我读到它是一个 Visual Studio 代码,您必须转到 Project.json
并添加 System.Windows.Forms
作为依赖项。我不知道写什么来添加它。我在网上搜索并搜索了库存溢出。我找不到任何东西。
我不知道要输入什么来添加它。
我可能是错的,但我认为您不能将 System.Windows.Forms
程序集用于 .Net Core 项目。我的 Visual Studio 出现问题,所以我无法通过 project.json
由于您希望通过控制台捕获用户的输入,并根据某些条件更改颜色 - 您必须自己手动执行此操作。
下面是一个完整的应用程序示例,展示了如何做到这一点。本质上,您必须评估输入控制台的每个字符并确定它是否为数字。如果它是一个数字,你必须将 cursor 移回 1 个位置,这样你就可以覆盖刚刚输入的值。在覆盖该值之前,您更改控制台前景色。
using System;
namespace ConsoleApp2
{
public class Program
{
public static void Main(string[] args)
{
// Set up an infinite loop that will allow us to forever type unless 'Q' is pressed.
while(true)
{
ConsoleKeyInfo pressedKey = Console.ReadKey();
// If Q is pressed, we quit the app by ending the loop.
if (pressedKey.Key == ConsoleKey.Q)
{
break;
}
// Handle the pressed key character.
OnKeyDown(pressedKey.KeyChar);
}
}
private static void OnKeyDown(char key)
{
int number;
// Try to parse the key into a number.
// If it fails to parse, then we abort and listen for the next key.
// It will fail if anything other than a number was entered since integers can only store whole numbers.
if (!int.TryParse(key.ToString(), out number))
{
return;
}
// If we get here, then the user entered a number.
// Apply our logic for handling numbers
ChangeColorOfPreviousCharacters(ConsoleColor.Green, key.ToString());
}
private static void ChangeColorOfPreviousCharacters(ConsoleColor color, string originalValue)
{
// Store the original foreground color of the text so we can revert back to it later.
ConsoleColor originalColor = Console.ForegroundColor;
// Move the cursor on the console to the left 1 character so we overwrite the character previously entered
// with a new character that has the updated foreground color applied.
Console.SetCursorPosition(Console.CursorLeft - 1, Console.CursorTop);
Console.ForegroundColor = color;
// Re-write the original character back out, now with the "Green" color.
Console.Write(originalValue);
// Reset the consoles foreground color back to what ever it was originally. In this case, white.
Console.ForegroundColor = originalColor;
}
}
}
控制台的输出将如下所示: