C# Console question?

            Console.SetCursorPosition(0, 0);            Console.Write("Hallo Welt");            Console.SetCursorPosition(0, 1);            Console.Write("Hallo Du");            Console.SetCursorPosition(0, 0);            Console.Write("Your custom message");

I print two messages: "Hello World" and "Hello You" (in two different lines)

Now I want the first message to be replaced with "Your custom message." I thought I'd just move the cursor, retype, and that would be it. I was wrong: The second message, "Hello You," then disappears! Why?

The console then consists only of the message "Your custom message." How do I resolve this?

(And no, I don't want to print "Hello You" again, that would mean that if I wanted to change something in the middle, I would have to cache all the text somehow.)

(1 votes)
Loading...

Similar Posts

Subscribe
Notify of
8 Answers
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
regex9
1 year ago

I can’t reproduce the problem in a Windows console. Instead, the expected target behavior occurs.

Possible reasons that seem to me why it doesn’t work with you:

  • You execute the application via a console application that can be used Author and SetCursorPosition different from the Windows console. Note that some IDEs (example: Eclipse) have integrated an own implementation of a console.
  • When updating the first line, the second line slides out of the visible window area. See if you can scroll in the console window.
  • You perform other instructions / actions that are not named in your question description.
regex9
1 year ago

I could reproduce it on another computer. Since the cursor is still in the first line, the following lines are overwritten by the system (as is the output of the context path after execution of the application).

Before the end of the program (or after the overwriting) you should put the cursor position back to the end (e.g. into the third line):

Console.SetCursorPosition(0, 2);
FaTech
1 year ago

What exactly is the problem? Does it work exactly as it should…

Maybe there’s another mistake you can’t find because there’s too little information. Or do I get something wrong?

Compiled and tested on Windows and Linux (Ubuntu)

FaTech
1 year ago
Reply to  FakeProfile

Both ways tested and running