tal
tal

Reputation: 295

Last line in console not overwrite

I want to overwrite the last line in the console for looping a specific row with changes.

For example: I want to print to console . then .. then ...

This my code for that:

int dot = 1;
    while (true)
    {
        switch (dot)
        {
            case 1:
                Console.SetCursorPosition(0, Console.CursorTop-1);
                Console.WriteLine(".");
                dot++;
                Thread.Sleep(500);
                break;
            case 2:
                Console.SetCursorPosition(0, Console.CursorTop -1);
                Console.WriteLine("..");
                dot++;
                Thread.Sleep(500);
                break;
            case 3:
                Console.SetCursorPosition(0, Console.CursorTop - 1);
                Console.WriteLine("...");
                dot = 1;
                Thread.Sleep(500);
                break;
        }
    }
}

My problem is that after the first complete round(after it print the "..." in first time) it not print "." the console stay on "..."

I mean:

now it stay "..." instead remove "..." and print ".".

Someone has idea why?

Upvotes: 2

Views: 1015

Answers (4)

Pranay Rana
Pranay Rana

Reputation: 176956

You want to replace ... after you last iteration , for that you can do like this, you can make use of PadRight function. PadRight is good way to go as you can configure number value in it.

var str = ".";
var padded = str.PadRight(2);
// padded = ".  "

so your code will become like this

switch (dot)
        {
            case 1:
                Console.SetCursorPosition(0, Console.CursorTop-1);
                 var str = ".";
                var padded = str.PadRight(2);
                Console.WriteLine(str);
                dot++;
                Thread.Sleep(500);
                break;
            case 2:
                Console.SetCursorPosition(0, Console.CursorTop -1);
                Console.WriteLine("..");
                dot++;
                Thread.Sleep(500);
                break;
            case 3:
                Console.SetCursorPosition(0, Console.CursorTop - 1);
                Console.WriteLine("...");
                dot = 1;
                Thread.Sleep(500);
                break;
        }

Upvotes: 0

Ishan Jain
Ishan Jain

Reputation: 765

Console.WriteLine automatically appends newline(\n) character at the end which results in the cursor jumping to next line.

You'll have to use Console.Write. It does not appends a new line character. But You'll have to put a \r at the end of your text to bring the cursor back to starting position.

So, Your Print Statement would look like

Console.Write(". \r");

Console.Write(".. \r");

Console.Write("...\r");

Upvotes: -1

Jon Skeet
Jon Skeet

Reputation: 1503439

You're only printing one character when you print a single dot - you're not affecting the rest of the line.

If you just change this:

Console.WriteLine(".");

to

Console.WriteLine(".  ");

then it'll remove any characters written by previous iterations. (That still won't clear anything else on the line though.)

(You may want to change the Console.WriteLine("..") to Console.WriteLine(".. ") for consistency but it won't make a difference as you've already printed the final space in the previous iteration.)

Note that you can remove repetition fairly simply too:

string[] dots = { ".  ", "..", "..." };
int index = 0;
while (true)
{
    Console.SetCursorPosition(0, Console.CursorTop -1);
    Console.WriteLine(dots[index]);
    index = (index + 1) % 3;
    Thread.Sleep(500);
}

Upvotes: 9

Rafal
Rafal

Reputation: 12629

While writing to console you replace characters but on first run there is nothing to replace so you see the change. On second loop you replace first dot with new dot and that does not produce a change on the screen. The simplest way to fix it would be to add spaces after dot to your first strings so that is is 3 characters long:

".  "

Upvotes: 0

Related Questions