0

i'm making some projects in C# but i cant make the console hold some information on the screen.

for example: i want to creat a shopping list were on the top show the total spend and on the bottom show the shop itens, were i buy something the top would update without ereasing everything but showing only the top part

there is a way to make this ?

  • When you work with console in non-streaming manner, you probably should use some "graphics" library for this. Two reasons: simplicity of usage and clean code base. For example - https://github.com/Haydend/ConsoleDraw – eocron Mar 05 '20 at 13:52

1 Answers1

1

Source Answer

Description

You can use the Console.SetCursorPosition function to go to a specific line number. Than 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

Dave
  • 50
  • 1
  • 7
  • 2
    Do not forget to lock such Console interactions. Multiple threads can write to console and your window will be scrumbled if you don't do so. Figuring out why everything is evaporated in console or nothing appearing is very hard. – eocron Mar 05 '20 at 13:55