2

Good Day

If this is the output of the Console:

.
.
.
.
Hello World!

I would like to set the cursor's position, say 20 lines from the last line of text. How would I do this in the case that the last line is obviously constantly changing?

Thank you for your time.

MarkusMulholland
  • 199
  • 2
  • 14
  • It is not terribly obvious why this would be useful, so pretty hard to be accurate. A for-loop that calls Console.WriteLine() 20 times gives you what you asked for. Seems too obvious a solution. – Hans Passant Aug 17 '18 at 10:10
  • Thank you Hans. Unfortunately, once the cursor has been set to, say 20 lines below, I would then need it to be set to 19 lines ahead of the original point, then 18 and so on, to give the illusion that the console is printing in an upward direction – MarkusMulholland Aug 17 '18 at 10:14
  • 1
    Pretty important to learn how to discover this yourself. In the text editor, put the caret on "Console" and press F1. That opens a web page that describes the class. Look through the properties and methods to see what the class can do. You'll easily find the CursorTop property for example. – Hans Passant Aug 17 '18 at 10:23
  • Thank you so much for that I will definitely use it in future. – MarkusMulholland Aug 17 '18 at 10:35

2 Answers2

1
Console.SetCursorPosition(Console.CursorLeft, (Console.CursorTop+20))

Passing CursorLeft keeps it at the same left position, if that's important to you. Otherwise you can just pass 0.

Console.SetCursorPosition(0, (Console.CursorTop+20))
Stuart
  • 3,949
  • 7
  • 29
  • 58
1
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;

namespace CUrsorPosition
{
    class Program
    {
       static int lastCursorLocation = 0;
       //customCursorPosition say which line number you want the cursor to //set after the last text for example i have set it to 1 so next line to hellow //world
       static int customCursorPosition = 1;
        static void Main(string[] args)
        {
            // int customCursorPosition = 0;
            WriteLine(".");
            WriteLine(".");
            WriteLine(".");
            WriteLine(".");

            int x = Console.CursorLeft;
            int y = Console.CursorTop;
            Console.CursorTop = Console.WindowTop + Console.WindowHeight - 1;
            Console.SetCursorPosition(x, y);
            WriteLine("Hello World");
            Console.SetCursorPosition(0, y+ customCursorPosition);
            Console.ReadLine();


        }
        static void WriteLine(string message, [CallerLineNumber] int lineNumber = 0)
        {
            Console.WriteLine(lineNumber + ": " + message);
        }
        static void WriteLine(string message)
        {
            StackFrame callStack = new StackFrame(1, true);
            var lineNumber = callStack.GetFileLineNumber();
            lastCursorLocation = callStack.GetFileLineNumber();
            Console.WriteLine(message);

        }
    }
}