Open E-School

Male
Friday, February 24, 1984

CSharp (C#) Console Application – Reading and setting the cursor position

Category: Programming Date: Wednesday, March 25, 2015

Goal

Learn how to control the cursor position in C# console application in order to decide the position where to write the text

Namespaces used

System – Console class is defined in the System namespace

Classes used

Console

Methods used

Console.SetCursorPosition

Properties used

Console.CursorLeft

Console.CursorTop

Brief description

When you write text on the console window using C# or any other programming language you notice that the cursor moves in a horizontal directly. Won’t it be nice to be able to control the cursor? To be able to tell the cursor the location on the console window where it should move so that you can write your text there. The console class in C# gives you an option to do so using the Cosole.SetCursorPosition method. Also you can ask the current position of the cursor from the Console class by using the Console.CursorLeft and Console.CursorTop properties.

Code

using System;    namespace ConsoleApplication1  {      class Program      {          public static void Main(string[] args)          {              for (int i = 0; i < 10; ++i)              {                  Console.SetCursorPosition(i, i);                  Console.Write("Cursor ({0},{1})", Console.CursorLeft, Console.CursorTop);              }            }      }  }

Output

CSharp (C#) Console Application – Reading and setting the cursor position 


Code explanation

Console.SetCursorPosition, Console.CursorLeft and Console.CursorTop are the three main heros in this program. You can see how we are setting the cursor positing by calling the Console.SetCursorPosition(i, i) code. Then next we are using Console.CursorLeft and Console.CursorTop properties to get the current cursor position and then print it.

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