103

While working on a question/answer program for school, it occurred to me that I can use Console.Clear() to wipe out everything on the screen. I wonder if I can use Console.Readline(valueOne), then output only the answer without the question. If I only asked one question, the Console.Clear works.

I have several questions with values not references, to erase if possible. I want to leave out the questions and only display several answers. I think if I store the answers, I could use Console.Clear() then just Console.WriteLine() with three variables. I could do something like this:

Console.WriteLine("Value 1 is: {0:c}" + "Value 2 is: {1:c}" + "Value 3 is: {2:c}, valueOne, valueTwo, valueThree).

The problem is easier with references because the values are stored and retrieved. If I simply use methods to pass by value and output the value, main() will not have a reference to those values to clear and output again. That's why I wonder if I can just ask a question, then erase the line and output only the answer (or answers).

I am just trying to understand the possibilities and not trying to setup a program. I like to know the abilities of outputting a value from reference and by value without extra output questions.

Jesse
  • 8,605
  • 7
  • 47
  • 57
bad boy
  • 1,133
  • 2
  • 7
  • 8

10 Answers10

185

Description

You can use the Console.SetCursorPosition function to go to a specific line number. Then you can use this function to clear the line:

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

Sample

Console.WriteLine("Test");
Console.SetCursorPosition(0, Console.CursorTop - 1);
ClearCurrentConsoleLine();

More Information

hellow
  • 12,430
  • 7
  • 56
  • 79
dknaack
  • 60,192
  • 27
  • 155
  • 202
  • 8
    This code should probably use `Console.BufferWidth` instead of `Console.WindowWidth`, since it's possible there will be characters written outside of the visible window area. – NathanAldenSr May 30 '16 at 03:55
  • Why does taking `(Console.CursorTop - 1)` not cause an out of range error? I'm looking at the C# source for [SetCursorPosition](https://referencesource.microsoft.com/#mscorlib/system/console.cs,114298628fe9afcf,references) and it looks like it uses 'int' which would make the number -1. I see that it doesn't use an unsigned integer, so why doesn't it throw an error? – The Fluffy Robot Jul 17 '17 at 13:33
  • @TheFluffyRobot because `Console.CursorTop` returns the current line, so `Console.CursorTop - 1` returns the previous line. Since there is a `Console.WriteLine("Test");` before, the current line will never be 0. – Magnetron Feb 13 '20 at 18:06
61

A simpler and imho better solution is:

Console.Write("\r" + new string(' ', Console.WindowWidth) + "\r");

It uses the carriage return to go to the beginning of the line, then prints as many spaces as the console is width and returns to the beginning of the line again, so you can print your own test afterwards.

hellow
  • 12,430
  • 7
  • 56
  • 79
  • 31
    For me it worked only with Console.WindowWidth-1: `Console.Write("\r" + new string(' ', Console.WindowWidth - 1) + "\r");` – Dorin Sep 04 '13 at 14:03
19

"ClearCurrentConsoleLine", "ClearLine" and the rest of the above functions should use Console.BufferWidth instead of Console.WindowWidth (you can see why when you try to make the window smaller). The window size of the console currently depends of its buffer and cannot be wider than it. Example (thanks goes to Dan Cornilescu):

public static void ClearLastLine()
{
    Console.SetCursorPosition(0, Console.CursorTop - 1);
    Console.Write(new string(' ', Console.BufferWidth));
    Console.SetCursorPosition(0, Console.CursorTop - 1);
}
User0123456789
  • 295
  • 3
  • 8
  • This should normally be a comment to the respective post(s) as it doesn't answer the question by itself. – Dan Cornilescu Apr 23 '16 at 16:49
  • 8
    I cannot answer to their posts when I do not have enough reputation, I cannot contact the authors of the comments, due to not finding their emails and there is no point of spamming the same (not original) solution in my answer. – User0123456789 Apr 23 '16 at 16:51
  • if (Console.CursorTop > 0) Console.SetCursorPosition(0, Console.CursorTop - 1); – Adronius Feb 26 '21 at 10:27
10

This worked for me:

static void ClearLine(){
    Console.SetCursorPosition(0, Console.CursorTop);
    Console.Write(new string(' ', Console.WindowWidth)); 
    Console.SetCursorPosition(0, Console.CursorTop - 1);
}
Community
  • 1
  • 1
bartburkhardt
  • 7,498
  • 1
  • 18
  • 14
8

My preferred method is to use PadRight. Instead of clearing the line first, this clears the remainder of the line after the new text is displayed, saving a step:

Console.CursorTop = 0;
Console.CursorLeft = 0;
Console.Write("Whatever...".PadRight(Console.BufferWidth));
John
  • 313
  • 5
  • 9
3

We could simply write the following method

public static void ClearLine()
{
    Console.SetCursorPosition(0, Console.CursorTop - 1);
    Console.Write(new string(' ', Console.WindowWidth));
    Console.SetCursorPosition(0, Console.CursorTop - 1);
}

and then call it when needed like this

Console.WriteLine("Test");
ClearLine();

It works fine for me.

ItsJ0el
  • 55
  • 1
  • 5
Alec
  • 31
  • 1
1

To clear from the current position to the end of the current line, do this:

    public static void ClearToEndOfCurrentLine()
    {
        int currentLeft = Console.CursorLeft;
        int currentTop = Console.CursorTop;
        Console.Write(new String(' ', Console.WindowWidth - currentLeft));
        Console.SetCursorPosition(currentLeft, currentTop);
    }
Jesse Chisholm
  • 3,857
  • 1
  • 35
  • 29
1
public static void ClearLine(int lines = 1)
{
    for (int i = 1; i <= lines; i++)
    {
        Console.SetCursorPosition(0, Console.CursorTop - 1);
        Console.Write(new string(' ', Console.WindowWidth));
        Console.SetCursorPosition(0, Console.CursorTop - 1);
    }
}
Pranav
  • 11
  • 1
0

I think I found why there are a few varying answers for this question. When the window has been resized such that it has a horizontal scroll bar (because the buffer is larger than the window) Console.CursorTop seems to return the wrong line. The following code works for me, regardless of window size or cursor position.

public static void ClearLine()
{
    Console.SetCursorPosition(0, Console.CursorTop);
    Console.Write(new string(' ', Console.WindowWidth));
    Console.SetCursorPosition(0, Console.CursorTop - (Console.WindowWidth >= Console.BufferWidth ? 1 : 0));
}

Without the (Console.WindowWidth >= Console.BufferWidth ? 1 : 0), the code may either move the cursor up or down, depending on which version you use from this page, and the state of the window.

0

This works perfect for me as i expected -

    public static void ClearLine()
    {
        Console.SetCursorPosition(0, Console.CursorTop);
        Console.Write(new string(' ', Console.WindowWidth));
        Console.SetCursorPosition(0, Console.CursorTop);
    }
s.k.paul
  • 7,099
  • 28
  • 93
  • 168