Can Console.Clear be used only to clear a string instead of the entire console?

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

I have a few questions with values ​​other than links to remove, if possible. I want to leave questions and display only a few answers. I think that if I save the answers, I could use Console.Clear() and 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 simpler with links, as values ​​are stored and retrieved. If I just use methods to pass by value and output the value, main() will not refer to these values ​​for cleaning and output again. This is why I am wondering if I can just ask a question and then delete the line and print only the answer (or answers).

I'm just trying to figure out the possibilities and not trying to tune the program. I like to know the possibilities of outputting the value from the link and the value without additional output questions.

+50
c #
Jan 20 '12 at 19:26
source share
9 answers

Description

You can use the Console.SetCursorPosition function to jump to a specific line number. How can you use this function to clear a string

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

Example

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

Additional Information

+99
Jan 20 '12 at 19:30
source share

Simple and best solution:

 Console.Write("\r" + new string(' ', Console.WindowWidth) + "\r"); 
+31
Dec 29 '12 at 17:03
source share

This worked for me:

 static void ClearLine(){ Console.SetCursorPosition(0, Console.CursorTop); Console.Write(new string(' ', Console.WindowWidth)); Console.SetCursorPosition(0, Console.CursorTop - 1); } 
+7
Jul 26 '13 at 11:29
source share

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

 public static void ClearLastLine() { Console.SetCursorPosition(0, Console.CursorTop - 1); Console.Write(new string(' ', Console.BufferWidth)); Console.SetCursorPosition(0, Console.CursorTop - 1); } 
+4
Apr 23 '16 at 16:41
source share

My preferred method is to use PadRight. Instead of clearing the line first, it clears the rest of the line after displaying the new text, saving the step:

 Console.CursorTop = 0; Console.CursorLeft = 0; Console.Write("Whatever...".PadRight(Console.BufferWidth)); 
+3
Jan 31 '17 at 19:08
source share

Can you just write this in the class

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

Then basically just type

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

This works great for me oo

+1
Sep 30 '13 at 23:04 on
source share

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

  public static void ClearToEndOfCurrentLine() { int currentLeft = Console.CursorLeft; int currentTop = Console.CursorTop; Console.Write(new String(' ', Console.WindowWidth - currentLeft)); Console.SetCursorPosition(currentLeft, currentTop); } 
+1
Mar 21 '15 at 20:08
source share
 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); } } 
0
Aug 03 '15 at 19:55
source share

I think I found why there are several different answers to this question. When the window was resized in such a way that it has a horizontal scroll bar (since 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 (Console.WindowWidth> = Console.BufferWidth? 1: 0), the code can move the cursor up or down depending on which version you are using on this page and the state of the window.

0
Mar 17 '16 at 4:10
source share



All Articles