12

I am writing a console program in C#.

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

Here's my issue:

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

GEOCHET
  • 21,119
  • 15
  • 74
  • 98
user47415
  • 123
  • 1
  • 5

3 Answers3

25

You could use a custom method to clear 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);
}
Diadistis
  • 12,086
  • 1
  • 33
  • 55
  • 1
    This is intriguing. I must check this out. – LeppyR64 Dec 18 '08 at 15:00
  • so if i use Console.SetWindowSize(width, height);, the width being 60, height 40. how would i go about clearing the bottom half of the screen? – user47415 Dec 18 '08 at 16:34
  • Cool! Since you're dealing with half of the screen, you avoid the Console.Clear command. You're still rewriting exactly half of the screen. – LeppyR64 Dec 18 '08 at 18:07
  • it works great, it keeps what i want. however the menu which clears, reappears below the cleared space. I need it to refresh in the cleared space. Any suggestions how i can modify the Clear method above to fix this? – user47415 Dec 18 '08 at 19:48
  • How about calling Console.SetCursorPosition right after the Clear method to put the cursor exactly in the spot you need to write? i.e.: Clear(0,20,60,20); Console.SetCursorPosition(0,20); – Diadistis Dec 18 '08 at 20:52
  • thanks very much Diadistis, really not familair with setting cursor positions etc... but you have been a great help. My program is running as I initially intended thanks to your help. – user47415 Dec 18 '08 at 21:10
  • lol. what we need is ncurses on windows. We also need a real console and not this 1961 era piece of crap; that can't be resized, nor change encoding on the fly, no multi tabs, no full screen, no font change dynamically, no transparency (could be done with a EnumWindow hack), no reasonable copy paste (solved in win10?), It's a joke. And I don't even get started on the shell features, the list is 20 times longer. and now my comment is much longer than I intended. – v.oddou Jul 02 '15 at 02:41
8

Edit: The answer here shows how you can manipulate the console much more powerfully than I was aware. What I originally wrote still applies, and might be useful if you don't have these facilities available, perhaps in a different language.


Store the logo and the initially generated state of the array and then when you clear the console write out the saved values again, rather than using the same generation procedure.

As others have said, the console isn't designed for this kind of operation so you have to work around it.

Another option might be to write to a string or a string builder rather than to the console directly, then whenever the console needs updating, clear it's output and write your "output" string / stream to the console. This would let you perform string operations/ regexes etc. on your output text to remove / leave specific things in place.

However, this would involve a lot of console overhead as you would end up reprinting the entire output on every update.

A solution to THAT might be to keep a parallel string / stream representation of what you write to the console, then when you need to clear you can clear the string representation, clear the console, then write the string out to the console again. That way you would add only an additional-write operation to your usual write, and the major extra work would happen only when you cleared the console.

Whatever you do, writing to the console is never a fast operation, and if you are doing lots of operations where stuff is written to the console, it can be a significant drag on the performance of your application.

Community
  • 1
  • 1
xan
  • 7,440
  • 8
  • 43
  • 65
1

Can you not clear and then re-write the logo and array? The console is not designed to be used as you describe.

LeppyR64
  • 5,251
  • 2
  • 30
  • 35