3

I need to clear a certain line in a console, but need to keep the rest. I know of Console.Clear(), but clears the whole console. How do I just clear one line?

braX
  • 11,506
  • 5
  • 20
  • 33
joesumbody122
  • 155
  • 2
  • 10
  • 1
    Possible duplicate - [Can Console.Clear be used to only clear a line instead of whole console?](http://stackoverflow.com/q/8946808/1012641) – Patrick D'Souza Apr 22 '13 at 15:49

1 Answers1

5

I have decided to answer my own question because I have googled this and nobody seems to have a method.

Since there wasn't a clearLine() / ClearLine() method, I made one.

Here it is:

private static void clearLine()
{
        Console.Write(new string(' ', Console.BufferWidth - Console.CursorLeft));
}

other possibilities:

private static void clearLine(int left, int top)
{    

int pLeft = Console.CursorLeft;
int pTop  = Console.CursorTop;

Console.setCursorPosition(left, top);
Console.Write(new string(' ', Console.BufferWidth - Console.CursorLeft));

Console.setCursorPosition(pLeft, pTop);
}
joesumbody122
  • 155
  • 2
  • 10
  • 1
    `Console.Write(new string(' ', Console.BufferWidth - (Console.CursorLeft + 1))` – SLaks Apr 22 '13 at 15:31
  • 1
    You should restore the cursor position when you finish. – SLaks Apr 22 '13 at 15:32
  • @SLaks: You have an error in your example, not enough closing brackets – musefan Apr 22 '13 at 15:33
  • The first one just clears the remainder current line; the method name should indicate that. – Servy Apr 22 '13 at 15:35
  • @SLaks good idea but you need another ) at the end I will change it in a moment – joesumbody122 Apr 22 '13 at 15:36
  • @Servy Thank you for pointing that out but SLaks point makes your statement false because I just tested it in my own program. – joesumbody122 Apr 22 '13 at 15:45
  • @joesumbody122 How does slack's point make my statement false? The first overload just clears the current line, so it would be best to have the name indicate it by calling it `clearCurrentLine` instead of `clearLine`. – Servy Apr 22 '13 at 15:48
  • If performance is an issue, you can adapt the answer I provided to the following question http://stackoverflow.com/questions/3173750/deleting-previously-written-lines-in-console/3174089#3174089 – Chris Taylor Apr 22 '13 at 15:49
  • @ChrisTaylor wow thats a lot more ... complex than mine. good job. – joesumbody122 Apr 22 '13 at 15:52
  • 1
    @joesumbody122, thanks. Btw. you do not need the for loop, the string you are creating already has the right amount of spaces to replace the remainder of the line. – Chris Taylor Apr 22 '13 at 16:04
  • Actually, I think you will be one short? It should be Console.Write(new string(' ', Console.BufferWidth - Console.CursorLeft)); – Chris Taylor Apr 22 '13 at 16:14
  • @ChrisTaylor the default buffer width is 80 and the console errors out if the cursor hits 80 so if you make it -1 (+1 since its being subtracted in the () ) it is before it hits the edge and doesn't error out – joesumbody122 Apr 22 '13 at 16:52
  • 1
    The left should be 0 to 79 you should validate the input. I you test your current code I am sure you will find that you are not clearing the last character on the line. – Chris Taylor Apr 22 '13 at 17:02
  • @ChrisTaylor ok I tested it and you were right. Now why were your right? – joesumbody122 Apr 23 '13 at 15:08
  • The width of the screen/buffer is 80 but it does not start from 1, it starts from 0. So for example when you clear a line starting from left 0, you need 80 spaces, but you where calculating 80 - (0 + 1) = 79 so you only cleared 79 characters from the row (0 - 78) leaving the last one. You can test this with other values for left. Does that help? – Chris Taylor Apr 23 '13 at 15:13