Reputation: 45
Is there any way that i can edit/rewrite certain lines that have already bin printed by the Console.PrintLine() method? I have to be able to edit any line that is shown in the prompt.
This is an example on what the code that i'm trying to get running, maybe can look like:
public static void RewriteLine(LineNr, Text)
{
//Code
}
Console.WriteLine("Text to be rewritten");
Console.Writeline("Just some text");
RewriteLine(1, "New text");
Example to show which line that i want rewritten based on output from the previous code:
Text to be rewritten //This line (has already bin executed by the Console.WriteLine() method) shall be replaced by: "New text"
Just some text
Upvotes: 1
Views: 2636
Reputation: 23078
It should look like this:
public static void RewriteLine(int lineNumber, String newText)
{
int currentLineCursor = Console.CursorTop;
Console.SetCursorPosition(0, currentLineCursor - lineNumber);
Console.Write(newText); Console.WriteLine(new string(' ', Console.WindowWidth - newText.Length));
Console.SetCursorPosition(0, currentLineCursor);
}
static void Main(string[] args)
{
Console.WriteLine("Text to be rewritten");
Console.WriteLine("Just some text");
RewriteLine(2, "New text");
}
What's happening is that you change cursor position and write there something. You should add some code for handling long strings.
Upvotes: 7