Open E-School

Male
Friday, February 24, 1984

C# console application – Displaying text in the same line by overwriting / updating the previously written text

Category: Programming Date: Wednesday, March 25, 2015

Goal

Learn how to update the text displayed in the console window and overwrite it with new text.

Namespaces used

System – Console class is defined in the System namespace

Classes used

Console

Method used

Console.SetCursorPosition - Static method defined in the Console class

Brief description

Suppose you are asked to create a console application to display a % progress indicator where you have to display progressing text in the same line by updating the earlier text then what will you do? You will use Console.Write or Console.WriteLine methods to write text on the console window. Both of them moves the cursor after displaying the text, Console.Write moves it to the next location and Console.WriteLine moves it to the next line. So when you want to write the next character your cursor is already moved and the character gets displayed at a new location.

So, what should be done?

If somehow you can ask the cursor to go back to the previous location and then write the text again you will be able to update the previously written line with the new line. But the question here is how you can do it.

Console.SetCursorPosition

The Console.SetCursorPosition method can come to your rescue. This methods takes the x and y location on the screen where you want to move your text. So you can ask your text to move to any location on the screen and write text there.

Use escape character

is also an option which you can use. But this option can help only if you want to keep writing in the same line. does not give you the power to move the cursor anywhere on the screen at will.

Let’s take an example

Look at the code below

using System;  using System.Threading;    namespace ConsoleApplication1  {      class Program      {          public static void Main(string[] args)          {              for (int i = 0; i <= 100; i++)              {                  Thread.Sleep(500);                  Console.SetCursorPosition(0, 0);                  Console.Write("{0}%   ", i);              }            }      }  }

Output

It’s hard to show the output of the above code because it will print 0 to 100 at the same location. So in the end all you will see is 100% written on the screen. It will be a good exercise for you to copy this code and run on your machine to see the output

Code explanation

In the code above there is a for loop which will print 0 to 100 on the screen. In a normal case it would have printed 0 to 100 in a single line because we are using Console.Write method to print the text on the screen. But the magic line of code in this program is Console.SetCursorPosition method. This method moves the cursor position to x=0 and y=0 every time the loop runs. This way we are keeping the cursor at the same location and overwriting the text.

Try this also

In the above program if you replace Console.SetCursorPosition(0,0) with Console.SetCursorPosition(i , i ) then you will see the following output
 
Notice how the text is being printed in a diagonally moving line just because you are incrementing the cursor position x and y coordinates in the loop.

Rate this article and help us improve

Please Login to rate
Overall ratings: 0 | Rating: out of 5
Previous Article Next Article

Quick Links

E-Magazines

@

Total Followers
Study Group Created
Study Group Joined
Following Teacher
Following Organization
Blog Articles Added
Questions Asked
Questions Answered
Jobs Posted
Total Members in Group
Questions asked by members
Tasks added in this Group

Please wait..

Ok

Login to Open ESchool OR Create your account    Login   SignUp