0

I've seen this question posted a few times but the questions I've visited don't have a solid answer for me. I've wrote a dummy application below just to try this out and I've noticed it has some issues.

It'll replace the new line with the next line in the list, the problem is if the previous line was longer than the new line you'll still be able to see the end of the previous line.

If you run the code below you'll see this at the end when it should be writing something along the lines of How would did your new hat cost? but writes something more like How would did your new hat cost?it? which includes the end of the previous line.

static void Main(string[] args)
{
    Console.CursorVisible = false;
    Console.WriteLine();

    var myLines = new List<string>
    {
        "I like dogs, but not cats.",
        "Want to get an icecream tomorrow?",
        "I would like to go to the park.",
        "If I was arrested, would you visit?",
        "How much did your new hat cost?"
    };

    foreach (var line in myLines)
    {
        Console.WriteLine($"  [{DateTime.Now.ToShortTimeString()}] Processing: " + line);
        Console.SetCursorPosition(0, Console.CursorTop - 1);
        Thread.Sleep(new Random().Next(200, 900));
    }

    Console.ReadKey(true);
}
carbin
  • 77
  • 7
  • 1
    You shouldn't instantiate a random in a loop like that. Use a static class variable – maccettura Apr 24 '18 at 18:06
  • This is just a dummy application, I realize newing up a random inside of a loop would not be good in production, although I was just writing it to get it working as an example. – carbin Apr 24 '18 at 18:08
  • Fair enough, but to answer your question. Setting the cursor position is one thing, but you never clear the line. Do a Console.WriteLine() after you set the cursor, then set it back to 0 – maccettura Apr 24 '18 at 18:09
  • 2
    Possible duplicate of [Can Console.Clear be used to only clear a line instead of whole console?](https://stackoverflow.com/questions/8946808/can-console-clear-be-used-to-only-clear-a-line-instead-of-whole-console) – maccettura Apr 24 '18 at 18:10
  • I've just tried doing `Console.WriteLine` after `Console.SetCursorPosition(0, Console.CursorTop - 1);` and then repeating the `SetCursorPosition` after `WriteLine` but it ends in the same result. – carbin Apr 24 '18 at 18:10
  • Write 80 blanks to the line (`Console.Write(new string(' ', 80));`) after setting the current position. – Ron Beyer Apr 24 '18 at 18:13
  • or write `line.PadRight(' ', Console.WindowWidth);` and enforce a max length of `Console.WindowWidth` at the same time – Cee McSharpface Apr 24 '18 at 18:16

1 Answers1

2

Modify your foreach loop to be like the following:

 foreach (var line in myLines)
        {
            Console.SetCursorPosition(0, Console.CursorTop - 1);
            ClearCurrentConsoleLine();
            Console.WriteLine($"  [{DateTime.Now.ToShortTimeString()}] Processing: " + line);
            Thread.Sleep(new Random().Next(200, 900));
        }

And use the following method:

public static void ClearCurrentConsoleLine()
    {
        int currentLineCursor = Console.CursorTop;
        Console.SetCursorPosition(0, Console.CursorTop);
        Console.Write(new string(' ', Console.WindowWidth));
        Console.SetCursorPosition(0, currentLineCursor);
    }

I have used to following to implement this solution: Can Console.Clear be used to only clear a line instead of whole console?

Mahmoud Heretani
  • 555
  • 1
  • 6
  • 17
  • If your answer is just a direct rip off of another answer, thats when you know this question is a duplicate and should be marked as such. – maccettura Apr 24 '18 at 18:17
  • OP wrote "the questions I've visited don't have a solid answer for me" but it is a duplicate no less. – Cee McSharpface Apr 24 '18 at 18:18