在c#控制台应用程序中,如何清除以前显示的文本,但在同一位置显示文本? [英] In c# console app, how can I display text at the same position while the previously displayed texts are cleared?
问题描述
在C#控制台应用程序中,我想在同一位置显示一些文本.虽然可以使用Console.SetCursorPosition()方法设置位置,但我不知道如何清除该位置之后的先前显示的文本(在该区域内具有给定宽度和高度的区域中的所有文本或部分文本)位置).你能帮我吗?
非常感谢.
In a C# Console application, I want to display some texts at the same position. While I can use the Console.SetCursorPosition() method to set the position, I have no idea that how can I clear the previously displayed text after that position(all text, or partial text within an area with a given width and height after that position). Can you help me?
Thanks a lot.
推荐答案
可以通过用空格字符覆盖来擦除现有文本.
要清除一行,顺序为
Existing text can be erased by overwriting with space characters.
To clear one line the sequence would be
SetCursor ROW, COLUMN
Write space * LINEWIDTH
并且可以通过增加循环内的ROW值轻松扩展为清除矩形区域.
如果还可以通过写退格字符将光标向左移动一个位置.序列退格键退格键将删除光标左侧的字符.
以下轮询循环演示了一种重复写入同一位置的简单方法.
and that can easily be extended to clear a rectangular area by incrementing the ROW value within a loop.
If it also possible to move the cursor one place to the left by writing a backspace character. The sequence backspace space backspace will erase the character to the left of the cursor.
The following polling loop demonstrates a simple way to write repeatedly to the the same location.
private void ShowProgress() {
String percent = "";
for(Int32 i = 0; i <= 100, i++) {
percent = String.Format(" Progress: {0}%", i);
Console.Write(percent);
Thread.Sleep(500);
for (Int32 j = 0; i < percent.Length; i++) {
Console.Write("\b \b"); // backspace - space - backspace
}
}
Console.WriteLine();
}
艾伦.
Alan.
我猜Console.Clear不是您要找的东西吗?
I guess Console.Clear is not what you are looking for?
要标记Nischalke:
是的.
To Mark Nischalke:
Yes.
这篇关于在c#控制台应用程序中,如何清除以前显示的文本,但在同一位置显示文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!