Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clear a specific line in a Console application [duplicate]

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?

like image 329
joesumbody122 Avatar asked Dec 17 '25 04:12

joesumbody122


1 Answers

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);
}
like image 63
joesumbody122 Avatar answered Dec 19 '25 22:12

joesumbody122



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!