C# - Overwrite Text in the console?

Started by
5 comments, last by Septopus 5 years, 4 months ago

Hi there...

If I print out stuff to the console... is there a way to print over that existing text... rather than printing again a new line...


Console.Write("This is a bunch of text that gets printed to screen");
Console.ReadKey();
Console.Write("****");

This should output..... (after the keyInput pause... So it over writes the existing text in the console instead of making a new line.

Quote

**** is a bunch of text that gets printed to the screen

Basically I am working on my 1st complete console project as I go through this book called The C# Players Guide and it is asking me to make a TicTacToe game.. I would like to modify the "map" instead of printing out entire new ones and scrolling the game upward..

So... I would like to be able to do the above code but get THIS output.

Quote

This is a bunch of **** that gets printed to screen

Now I am sure it can do this, but my unfamiliarity with the subject makes googling hard as tons of info comes up not related to what I am asking. I would imagine is a char[][] that represents the console window and I could use this to print at any point of the window with Console.Write(); ... but I do not know exactly how, or how I can make a char[][] as diffrent users may have a different sized console window and stuff.

TL;DR - How can I write into the console and replace existing text in the console instead of adding a new line or new chars at the end of an existing line?

: GameDev.Net Blog: Open Blog; Read Blog : My uTube Chans : TragicTableTopTragicDeskTop
Advertisement

I'm sorry I do not understand what you mean by that....

I am using a thing I wrote I am calling a inputbuffer that assembles all the text before it is printed... but at the moment every time I print it adds new lines or add to the end of existing lines. Like in a tictactoe game you start with a screen that looks like a 3x3 grid... when a player makes a move, one of those grid points fills... at the moment my game works but the new "frame" so to speak showing the new move is printed to the console and pushes all the other stuff up... as in you can scroll up the console to see old print outs..

What I am asking is how can I, instead, of adding new lines or stuff to the end of existing lines, to over write stuff that is already printed out.

: GameDev.Net Blog: Open Blog; Read Blog : My uTube Chans : TragicTableTopTragicDeskTop

Is this what you seek?

https://docs.microsoft.com/en-us/dotnet/api/system.console.setcursorposition?view=netframework-4.7.2#System_Console_SetCursorPosition_System_Int32_System_Int32_

3 minutes ago, Septopus said:

That looks like the stuff man.. thanks. I'll poke around google using those terms... see if that leads me to a way to get the current console dimensions or set the console dimensions.. as "set potions" would require me to KNOW the positions or w/e right.

thanks

**EDIT** - actually there are a bunch of methods listed there that dose exactly that.. great man.. thanks I can finish up this exercise now.. thank you!

: GameDev.Net Blog: Open Blog; Read Blog : My uTube Chans : TragicTableTopTragicDeskTop
1 minute ago, fleabay said:

I would think that Console.SetCursorPosition() would cause problems because of the scrolling.

Just clear the screen and redraw the board.

Wouldn't that kind of "flash" as it refreshes... the cursor position thing should be seamless.. I just need to build it so it all exists on a single screen with no scrolling.... I will try that idea first though. Thank you.

: GameDev.Net Blog: Open Blog; Read Blog : My uTube Chans : TragicTableTopTragicDeskTop

The console should only scroll when you output a new line character../etc.. at the end of the "buffer"..

This topic is closed to new replies.

Advertisement