0

I am working on a small console application written in C#. I want to delete / clear the last line in the console.

My problem: The application behaves differently outside of the IDE (VS 2019).

The following code is working inside the IDE:

Console.SetCursorPosition(0, Console.CursorTop - 1);
Console.Write(new string(' ', Console.BufferWidth));
Console.SetCursorPosition(0, Console.CursorTop);

The follolloing code is working outside the IDE:

Console.SetCursorPosition(0, Console.CursorTop - 1);
Console.Write(new string(' ', Console.BufferWidth));
Console.SetCursorPosition(0, Console.CursorTop - 1);

I did some resach and found out that there are two properties regarding the problem. The first one is Console.WindowWidth and the second one is Console.BufferWidth.

Furthermore I found a solution on the internet:

Console.SetCursorPosition(0, Console.CursorTop - 1);
Console.Write(new string(' ', Console.WindowWidth));
Console.SetCursorPosition(0, Console.CursorTop - (Console.WindowWidth >= Console.BufferWidth ? 1 : 0));

-> This solution did not work for me.

I did some further investigation and realised that the cursor behaves differently. Interestingly Console.WindowWidth and the Console.BufferWidth properties where the same all the time (Found it out via

Console.WriteLine("WindowWidth = {0} BufferWidth = {1}", Console.WindowWidth, Console.BufferWidth);

I am fairly new to C# that is why I need your help. Could you explain me the difference between buffered and none buffered and also provide me with some solutions?

Thanks a LOT!!

Tom Buente
  • 11
  • 5
  • Possible duplicate of [Can Console.Clear be used to only clear a line instead of whole console?](https://stackoverflow.com/questions/8946808/can-console-clear-be-used-to-only-clear-a-line-instead-of-whole-console) –  Jul 26 '19 at 20:47
  • Yeah I allready read the post but it did not help me. – Tom Buente Jul 26 '19 at 20:55
  • The console class is documented, along with the [`WindowWidth`](https://learn.microsoft.com/en-us/dotnet/api/system.console.windowwidth?view=netframework-4.8) and [`BufferWidth`](https://learn.microsoft.com/en-us/dotnet/api/system.console.bufferwidth?view=netframework-4.8) properties, so if you've still got a question about the difference, please be more specific. FYI, "buffer" is the size of data that is kept in a cache outside of the window bounds. See [this page](https://learn.microsoft.com/en-us/windows/console/consoles) for more details. – Rufus L Jul 26 '19 at 21:31
  • Can you describe specifically what it is you want to achieve, and how the different things you've tried behave in the different environments? It seems like you may just need to capture the cursor top in a variable before writing the blank line, so that in both cases you set the cursor to the correct position. – Rufus L Jul 26 '19 at 21:44
  • I allready read the docs but I still do not understand the difference. English is not my first language. @RufusL – Tom Buente Jul 26 '19 at 21:45
  • The buffer holds characters, and they are displayed in the window. If the buffer is larger than the window, then you'll have to scroll (either horizontally, vertically, or both depending on if the buffer is wider and/or taller than the window) to see the text. – Rufus L Jul 26 '19 at 21:47
  • Note that it's not possible to set the buffer size to be smaller than the window size (right click on the title bar of the console window, click properties, then go to the layout tab) – Rufus L Jul 26 '19 at 22:06

1 Answers1

2

It sounds like you need to capture the cursor top before running your code, since it appears that the top is different after you write the blank line in some cases.

This should solve the problem, but if it doesn't please clarify specifically what behavior you want, and what behavior you're seeing that's not correct.

// Capture current cursor position
var cursorTop = Console.CursorTop;
var cursorLeft = Console.CursorLeft;

// Clear the previous line (above the current position)
Console.SetCursorPosition(0, cursorTop - 1);
Console.Write(new string(' ', Console.BufferWidth));

// Resume cusor at it's original position
Console.SetCursorPosition(cursorLeft, cursorTop);
Rufus L
  • 36,127
  • 5
  • 30
  • 43