Reputation: 339
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.
Upvotes: 2
Views: 2591
Reputation: 25
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);
}
}
}
Upvotes: 1
Reputation: 4258
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))
Upvotes: 1