1

I want a user of my console application could update text which was written by Console.Write(); For example:

var currentVar="bla-bla-bla";
Console.Write(currentVar);
var newVar=Console.Read();//here user can update previous output of Write() method;
Console.Write(newVar);//output of updated value

Is it possible?

SimonD
  • 1,441
  • 2
  • 18
  • 30
  • The quickest way would be to clear the console, then display the the text all together with the edits.... – TheGeekZn May 10 '13 at 11:23

3 Answers3

1

Console.Read doesn't read the previous output, instead it wait for the input.

Console.Read Method - MSDN

Reads the next character from the standard input stream.

The Read method blocks its return while you type input characters; it terminates when you press the Enter key. Pressing Enter appends a platform-dependent line termination sequence to your input (for example, Windows appends a carriage return-linefeed sequence). Subsequent calls to the Read method retrieve your input one character at a time. After the final character is retrieved, Read blocks its return again and the cycle repeats.

If you intentd to overwrite previous output with the new content, then use Console.SetCursorPosition. Also see this answer

Community
  • 1
  • 1
Habib
  • 219,104
  • 29
  • 407
  • 436
  • So what can I do to modify previous output? – SimonD May 10 '13 at 10:36
  • @SimonD Are you trying to overwrite the previous output so that the user can see the new entry on the previous line? – CathalMF May 10 '13 at 10:41
  • 1
    @SimonD, you can't read previous lines from the ouptut console, instead you can set Cursor to a position and write there, see [this answer](http://stackoverflow.com/a/888559/961113), use `Console.SetCursorPosition(0, Console.CursorTop);` – Habib May 10 '13 at 10:42
1

I think this is what you are trying to do.

    Console.WriteLine("Original");

    var newVar=Console.Read();

    Console.SetCursorPosition(0, Console.CursorTop -2); // Where -2 moves the cursor two lines up.

    Console.WriteLine(newVar);

Then however you will be overwriting the next lines because the cursor will just move down. You will need to use the COnsole.SetCursorPosition again to put the cursor back to where you want it.

CathalMF
  • 9,705
  • 6
  • 70
  • 106
0

You should replace StandardOutput by your own implementation and catch the written text, then do whatever you want with it...