C# Console.Clear 可以用来只清除一行而不是整个控制台吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8946808/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can Console.Clear be used to only clear a line instead of whole console?
提问by bad boy
While working on a question/answer program for school, it occurred to me that I can use Console.Clear()to wipe out everything on the screen. I wonder if I can use Console.Readline(valueOne), then output only the answer without the question. If I only asked one question, the Console.Clearworks.
在为学校编写问答程序时,我Console.Clear()突然想到我可以用它来清除屏幕上的所有内容。我想知道我是否可以使用Console.Readline(valueOne),然后只输出没有问题的答案。如果我只问一个问题,那就行了Console.Clear。
I have several questions with values not references, to erase if possible. I want to leave out the questions and only display several answers. I think if I store the answers, I could use Console.Clear()then just Console.WriteLine()with three variables. I could do something like this:
我有几个问题是值而不是引用,如果可能,请删除。我想省略问题,只显示几个答案。我想如果我存储答案,我可以Console.Clear()只Console.WriteLine()使用三个变量。我可以做这样的事情:
Console.WriteLine("Value 1 is: {0:c}" + "Value 2 is: {1:c}" + "Value 3 is: {2:c}, valueOne, valueTwo, valueThree).
The problem is easier with references because the values are stored and retrieved. If I simply use methods to pass by value and output the value, main()will not have a reference to those values to clear and output again. That's why I wonder if I can just ask a question, then erase the line and output only the answer (or answers).
引用的问题更容易,因为值是存储和检索的。如果我只是使用方法按值传递并输出值,main()将不会有对这些值的引用来清除和再次输出。这就是为什么我想知道我是否可以只问一个问题,然后擦除该行并仅输出答案(或答案)。
I am just trying to understand the possibilities and not trying to setup a program. I like to know the abilities of outputting a value from reference and by value without extra output questions.
我只是想了解可能性,而不是尝试设置程序。我想知道从引用和按值输出值的能力,而无需额外的输出问题。
采纳答案by dknaack
Description
描述
You can use the Console.SetCursorPositionfunction to go to a specific line number.
Than you can use this function to clear the line
您可以使用该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);
}
Sample
样本
Console.WriteLine("Test");
Console.SetCursorPosition(0, Console.CursorTop - 1);
ClearCurrentConsoleLine();
More Information
更多信息
回答by hellow
A simpler and imho better solution is:
一个更简单和更好的解决方案是:
Console.Write("\r" + new string(' ', Console.WindowWidth) + "\r");
It uses the carriage returnto go to the beginning of the line, then prints as many spaces as the console is width and returns to the beginning of the line again, so you can print your own test afterwards.
它使用回车转到行首,然后打印与控制台宽度一样多的空格,然后再次返回到行首,以便您可以在之后打印自己的测试。
回答by bartburkhardt
This worked for me:
这对我有用:
static void ClearLine(){
Console.SetCursorPosition(0, Console.CursorTop);
Console.Write(new string(' ', Console.WindowWidth));
Console.SetCursorPosition(0, Console.CursorTop - 1);
}
回答by Alec
We could simply write the following method
我们可以简单地编写以下方法
public static void ClearLine()
{
Console.SetCursorPosition(0, Console.CursorTop - 1);
Console.Write(new string(' ', Console.WindowWidth));
Console.SetCursorPosition(0, Console.CursorTop - 1);
}
and then call it when needed like this
然后在需要时像这样调用它
Console.WriteLine("Test");
ClearLine();
It works fine for me.
这对我来说可以。
回答by Jesse Chisholm
To clear from the current position to the end of the current line, do this:
要从当前位置清除到当前行的末尾,请执行以下操作:
public static void ClearToEndOfCurrentLine()
{
int currentLeft = Console.CursorLeft;
int currentTop = Console.CursorTop;
Console.Write(new String(' ', Console.WindowWidth - currentLeft));
Console.SetCursorPosition(currentLeft, currentTop);
}
回答by Pranav
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);
}
}
回答by SomeNameNotFake
I think I found why there are a few varying answers for this question. When the window has been resized such that it has a horizontal scroll bar (because 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.
我想我找到了为什么这个问题有几个不同的答案。当窗口被调整大小使其具有水平滚动条时(因为缓冲区比窗口大) 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));
}
Without the (Console.WindowWidth >= Console.BufferWidth ? 1 : 0), the code may either move the cursor up or down, depending on which version you use from this page, and the state of the window.
如果没有 (Console.WindowWidth >= Console.BufferWidth ? 1 : 0),代码可能会向上或向下移动光标,具体取决于您从此页面使用的版本以及窗口的状态。
回答by User0123456789
"ClearCurrentConsoleLine", "ClearLine" and the rest of the above functions should use Console.BufferWidth instead of Console.WindowWidth (you can see why when you try to make the window smaller). The window size of the console currently depends of its buffer and cannot be wider than it. Example (thanks goes to Dan Cornilescu):
"ClearCurrentConsoleLine"、"ClearLine" 和上述其他函数应该使用 Console.BufferWidth 而不是 Console.WindowWidth (当您尝试使窗口变小时,您就会明白为什么)。控制台的窗口大小当前取决于它的缓冲区,不能比它宽。示例(感谢 Dan Cornilescu):
public static void ClearLastLine()
{
Console.SetCursorPosition(0, Console.CursorTop - 1);
Console.Write(new string(' ', Console.BufferWidth));
Console.SetCursorPosition(0, Console.CursorTop - 1);
}
回答by John
My preferred method is to use PadRight. Instead of clearing the line first, this clears the remainder of the line after the new text is displayed, saving a step:
我的首选方法是使用 PadRight。这不是先清除行,而是在显示新文本后清除行的其余部分,从而节省了一个步骤:
Console.CursorTop = 0;
Console.CursorLeft = 0;
Console.Write("Whatever...".PadRight(Console.BufferWidth));

