hi_there
hi_there

Reputation: 51

C#, SetCursorPosition deletes previous Positions content on the console

I can navigate on the console through arrowkeys. I use SetCursorPostion(CursorLeft, CursorTop) to move around on a generated/drawn "board", but the problem is when I press for example "DownArrow" it will delete/reset the content in the current position and jump to the position below.

Here is an example:

static void Main(string[] args)
    {
        Console.SetCursorPosition(5, 5);
        for (int i = 0; i < Console.BufferWidth; i++)
        {
            for (int n = 0; n < 50; n++)
            {
                Console.Write("X");
            }
        }

        Console.SetCursorPosition(20, 20);
        do 
        {
            ConsoleKey key = Console.ReadKey().Key;
            switch (key)
            {
                case ConsoleKey.UpArrow:
                    Console.SetCursorPosition(Console.CursorLeft - 1, Console.CursorTop - 1);
                    break;
                case ConsoleKey.DownArrow:
                    Console.SetCursorPosition(Console.CursorLeft - 1, Console.CursorTop + 1);
                    break;
                case ConsoleKey.RightArrow:
                    Console.SetCursorPosition(Console.CursorLeft, Console.CursorTop);
                    break;
                case ConsoleKey.LeftArrow:
                    Console.SetCursorPosition(Console.CursorLeft - 2, Console.CursorTop);
                    break;
            } 
        } while (true);
    }

Upvotes: 1

Views: 397

Answers (1)

hi_there
hi_there

Reputation: 51

Okay, all I had to do was to put "true" in Console.ReadKey().Key

Upvotes: 1

Related Questions