Kevin K
Kevin K

Reputation: 81

c# adding new lines to a console above old lines

    static void Main(string[] args)
    {
        int top, left;
        Console.WriteLine();
        Console.Write("Type here: ");

        for (int i = 0; i < 99; i++)
        {
            top = Console.CursorTop;
            left = Console.CursorLeft;

            Console.SetCursorPosition(0, Console.CursorTop - 1);
            Console.WriteLine("hi" + Environment.NewLine);
            Console.SetCursorPosition(left, top);         
        }


        Console.ReadKey();
    }

I want the result to have 100 "hi" above one "Type Here: " and I want to be able to output the "Type Here: " before printing out the hellos I would love any help as I have been stuck with this for a while.

Upvotes: 4

Views: 1729

Answers (1)

Rainer Schaack
Rainer Schaack

Reputation: 1618

IMHO mixing normal "stdio" output and cursor / window / buffer manipulation not a good idea, but anyway. Try this:

    static void Main(string[] args)
    {
        int top;
        Console.WriteLine();
        Console.Write("Type here: ");

        Console.WriteLine();

        for (int i = 0; i < 99; i++)
        {
            Console.WriteLine("hi" + Environment.NewLine);
        }

        top = Console.CursorTop;
        Console.MoveBufferArea(0, top - 199, Console.WindowWidth, 1, 0, top);

        Console.SetCursorPosition(11, Console.CursorTop);

        Console.ReadKey();

    }

Be aware that this is highly dependent on console buffer settings. You should consider setting the buffer with e.g.

Console.SetBufferSize(80, 500);

Upvotes: 4

Related Questions