想要根据输入有条件地更改控制台前景色

时间:2016-07-13 07:20:05

标签: c# visual-studio-code .net-core

我正在尝试使KeyDown语句有效。所以我在写:

[{"name":"A", "points": 9},
{"name":"A", "points": 5},
{"name":"A", "points": 4},
{"name":"A", "points": 1},
{"name":"A", "points": 3},
{"name":"A", "points": 6},
{"name":"B", "points": 5},
{"name":"B", "points": 1},
{"name":"B", "points": 2},
{"name":"B", "points": 3}]

Lowest points - typeA
Second lowest points - typeB

[{"name":"A", "points": 1, "type":"typeA"}, {"name":"A", "points": 3, "type":"typeB"}, {"name":"B", "points": 1, "type":"typeA"}, {"name":"B", "points": 2, "type":"typeB"}] 无法正常工作我读到它是一个Visual Studio Code,你必须转到private void KeyDown(object sender, System.Windows.Forms.KeyEventArgs e); 并添加System.Windows.Forms作为依赖项。我不知道写什么来添加它。我在网上搜索并搜索了库存溢出。我找不到任何东西。

我不知道要输入什么来添加它。

1 个答案:

答案 0 :(得分:0)

我可能错了,但我不相信你可以将System.Windows.Forms程序集用于.Net Core项目。我的Visual Studio正在执行,因此我无法通过project.json imports feature尝试使用它。话虽如此,它无论如何也无法满足你的需求。

由于您希望通过控制台捕获用户的输入,并根据某些条件更改颜色 - 您必须自己手动执行此操作。

以下是一个完整的应用程序示例,说明了如何执行此操作。基本上,您必须评估输入控制台的每个字符,并确定它是否为数字。如果是数字,则必须将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;
        }
    }
}

控制台的输出如下所示:

Result