bad boy
bad boy

Reputation: 1173

Can Console.Clear be used to only clear a line instead of whole console?

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.

Upvotes: 107

Views: 157810

Answers (11)

Alex
Alex

Reputation: 21

The cleanest solution I came to:

var cursorLeft = Console.CursorLeft;
Console.CursorLeft = 0;
Console.Write(new string(' ', cursorLeft));
Console.CursorLeft = 0;

Writing the whole Console.BufferWidth, not Console.BufferWidth - 1 may result in moving to the next line, depending on the console.

Upvotes: 1

dknaack
dknaack

Reputation: 60556

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

Upvotes: 196

s.k.paul
s.k.paul

Reputation: 7301

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);
    }

Upvotes: 0

Alec
Alec

Reputation: 41

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.

Upvotes: 4

hellow
hellow

Reputation: 13450

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.

Upvotes: 63

bartburkhardt
bartburkhardt

Reputation: 7768

This worked for me:

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

Upvotes: 11

John
John

Reputation: 343

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));

Upvotes: 8

User0123456789
User0123456789

Reputation: 303

"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);
}

Upvotes: 19

SomeNameNotFake
SomeNameNotFake

Reputation: 1

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.

Upvotes: 0

Pranav
Pranav

Reputation: 11

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);
    }
}

Upvotes: 1

Jesse Chisholm
Jesse Chisholm

Reputation: 4026

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);
    }

Upvotes: 1

Related Questions