简体   繁体   中英

C# - Rewrite/edit line while program is running

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM