C # console, Console.Clear problem

I am writing a console program in C #.

Is there a way I can use Console.Clear () to clear certain things on the console screen?

Here is my problem:

I have a logo (I put it on the screen using Console.WriteLine ()) and a 2d array that I want to keep constant and clear everything under it.

+11
c # console-application
Dec 18 '08 at 14:02
source share
3 answers

You can use a special method to clean parts of the screen ...

static void Clear(int x, int y, int width, int height) { int curTop = Console.CursorTop; int curLeft = Console.CursorLeft; for (; height > 0;) { Console.SetCursorPosition(x, y + --height); Console.Write(new string(' ',width)); } Console.SetCursorPosition(curLeft, curTop); } 
+21
Dec 18 '08 at 14:41
source share

Edit : The answer here shows how you can manipulate the console much more powerful than I knew. What I originally wrote is still applicable and may be useful if you do not have available tools, possibly in another language.




Save the logo and the initially generated state of the array, and then when you clear the console, write down the saved values ​​again, instead of using the same generation procedure.

As others have said, the console is not designed for this kind of operation, so you have to get around it.

Another option would be to write to a string or string constructor, rather than directly to the console, and then whenever the console needs to be updated, clear it and write the output / stream string to the console. This will allow you to perform string operations / regular expressions, etc. In the text of the output, to remove / leave certain things in place.

However, this will be associated with a lot of console overhead, as you end up retyping the entire output with each update.

A THAT solution might be to keep a parallel view of the line / stream of what you write on the console, and then when you need to clear, you can clear the string view, clear the console, and then write the line to the console again. That way, you would only add an extra write operation to your regular record, and most of the extra work would only happen when the console was cleaned up.

No matter what you do, writing to the console is never a quick operation, and if you perform many operations when writing to the console, it can significantly affect the performance of your application.

+8
Dec 18 '08 at 14:16
source share

Can you not clear and then rewrite the logo and array? The console is not intended for use in the description.

+1
Dec 18 '08 at 14:06
source share



All Articles