【问题标题】:Is possible to the comand Console.Clear() only clear a specific part of the console命令 Console.Clear() 可以只清除控制台的特定部分
【发布时间】:2020-03-05 13:48:09
【问题描述】:

我正在用 C# 制作一些项目,但我无法让控制台在屏幕上保存一些信息。

例如:我想创建一个购物清单,顶部显示总支出,底部显示商店数量,如果我购买的东西顶部会更新而不删除所有内容,但只显示顶部

有没有办法做到这一点?

【问题讨论】:

  • 当您以非流式方式使用控制台时,您可能应该为此使用一些“图形”库。两个原因:使用简单和干净的代码库。例如 - github.com/Haydend/ConsoleDraw

标签: c# .net console console-application


【解决方案1】:

来源Answer

说明

您可以使用Console.SetCursorPosition 函数转到特定的行号。 比你可以用这个功能来清线

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

示例

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

更多信息

【讨论】:

  • 不要忘记锁定此类控制台交互。多个线程可以写入控制台,如果您不这样做,您的窗口将被打乱。弄清楚为什么所有东西都在控制台中消失或什么都没有出现是非常困难的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-08-29
  • 2017-03-17
  • 2016-02-08
  • 1970-01-01
  • 1970-01-01
  • 2010-09-27
  • 2016-01-13
相关资源
最近更新 更多