Console.Clear能否只清除一行而不是整个控制台?

103
当我在为学校编写问答程序时,想到可以使用Console.Clear()清除屏幕上的所有内容。我想知道是否可以使用Console.Readline(valueOne),然后仅输出回答而不包括问题。如果我只问了一个问题,Console.Clear就可以工作。
我有几个没有引用值的问题需要擦除,如果可能的话。我想留下答案并仅显示几个答案。我认为如果我存储答案,我可以使用Console.Clear()然后只用三个变量调用Console.WriteLine()。我可以这样做:
Console.WriteLine("Value 1 is: {0:c}" + "Value 2 is: {1:c}" + "Value 3 is: {2:c}, valueOne, valueTwo, valueThree).
问题使用引用变量更容易,因为值被存储和检索。如果我只是使用传值方式输出值,main()将不会有对这些值的引用来清除并重新输出。那就是为什么我想知道是否可以只询问一个问题,然后删除该行并仅输出答案(或答案)。
我只是想了解可能性,而不是设置程序。我喜欢了解从引用和传值中输出值的能力,而无需额外的输出问题。

1
顺便说一下,我只是一个学习C#三周的学生。 - bad boy
1
你在尝试做什么?我很难理解你对“答案”和“问题”的使用 - 它们是什么,你正在构建什么?那么你的问题是什么? - Michael Banzon
10个回答

186

Description

您可以使用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();

更多信息


8
这段代码应该使用Console.BufferWidth而不是Console.WindowWidth,因为有可能会写入到可见窗口区域外的字符。 - NathanAldenSr
为什么使用 (Console.CursorTop - 1) 不会导致范围错误?我正在查看 SetCursorPosition 的 C# 源代码,它似乎使用了 'int',这将使数字为-1。我看到它没有使用无符号整数,那么为什么不会抛出错误? - The Fluffy Robot
@TheFluffyRobot 因为 Console.CursorTop 返回当前行,所以 Console.CursorTop - 1 返回上一行。由于之前有一个 Console.WriteLine("Test");,所以当前行永远不会是0。 - Magnetron

61

一种更简单、个人认为更好的解决方案是:

Console.Write("\r" + new string(' ', Console.WindowWidth) + "\r");

它使用回车符来回到行首,然后打印与控制台宽度相同数量的空格,并再次回到行首,这样你就可以在后面打印自己的测试。


31
对我而言,只有使用Console.WindowWidth-1才能生效: Console.Write("\r" + new string(' ', Console.WindowWidth - 1) + "\r"); - Dorin

19

"ClearCurrentConsoleLine","ClearLine"以及上述其余函数应该使用Console.BufferWidth而不是Console.WindowWidth(当你尝试缩小窗口时,您可以看出为什么)。控制台的窗口大小当前取决于其缓冲区,因此不能更宽。

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

这通常应该是对相应帖子的评论,因为它本身并不能回答问题。 - Dan Cornilescu
8
当我的声望不够时,我无法回复他们的帖子;由于找不到评论作者的电子邮件,我也无法与他们联系。在我的回答中重复(非原创)的解决方案没有意义。 - User0123456789
如果 (Console.CursorTop > 0) 则 Console.SetCursorPosition(0, Console.CursorTop - 1); - Adronius

10

这对我有用:

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

8

我的首选方法是使用 PadRight。与先清除行不同,这将在新文本显示后清除该行的其余部分,节省了一步操作:

Console.CursorTop = 0;
Console.CursorLeft = 0;
Console.Write("Whatever...".PadRight(Console.BufferWidth));

2
(BufferWidth -1) 以避免超出行宽。 - riezebosch

3
我们可以简单地编写以下方法。
public static void ClearLine()
{
    Console.SetCursorPosition(0, Console.CursorTop - 1);
    Console.Write(new string(' ', Console.WindowWidth));
    Console.SetCursorPosition(0, Console.CursorTop - 1);
}

然后在需要的时候像这样调用它

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

对我来说它很好用。


1
要清除从当前位置到当前行末尾的内容,请执行以下操作:
    public static void ClearToEndOfCurrentLine()
    {
        int currentLeft = Console.CursorLeft;
        int currentTop = Console.CursorTop;
        Console.Write(new String(' ', Console.WindowWidth - currentLeft));
        Console.SetCursorPosition(currentLeft, currentTop);
    }

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

3
请解释您的答案,以便于未来的读者能够受益。 - Prudhvi

0

我想我找到了为什么这个问题有几个不同答案的原因。当窗口被调整大小以至于它有一个水平滚动条(因为缓冲区比窗口大)时,Console.CursorTop似乎返回错误的行。以下代码适用于我,无论窗口大小或光标位置如何。

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

如果没有 (Console.WindowWidth >= Console.BufferWidth ? 1 : 0),代码可能会根据您从此页面使用的版本和窗口状态而向上或向下移动光标。


0
这对我来说完美无缺,正如我所预期的一样。
    public static void ClearLine()
    {
        Console.SetCursorPosition(0, Console.CursorTop);
        Console.Write(new string(' ', Console.WindowWidth));
        Console.SetCursorPosition(0, Console.CursorTop);
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接