Posted by: Cirilo Meggiolaro | 02/16/2009

Tip of the day #125 – Console class

Console application usually is my choice whenever I need to test code snippets or create design patterns concept proof because it’s fast to create and the output is pretty straightforward.

Let’s check today the properties, events and methods available for the Console class that may help you on either style or functionality:

Properties

  • ConsoleColor BackgroundColor: Gets or sets the background color of the console. It does not change the whole background but the area that has a text on it. If you want to change the entire background color, call the Clear method after you have changed the BackgroundColor property;
  • int BufferHeight: Gets or sets the height of the buffer area;
  • int BufferWidth: Gets or sets the width of the buffer area;
  • int CapsLock: Read-only property that retrieves a value indicating whether the CAPS LOCK key is turned on or turned off;
  • int CursorLeft: Gets or sets the column position of the cursor within the buffer area;
  • int CursorSize: Gets or sets the height of the cursor within a character cell;
  • int CursorTop: Gets or sets the row position of the cursor within the buffer area;
  • bool CursorVisible: Gets or sets a value indicating whether the cursor is visible;
  • TextWriter Error: Read-only property that retrieves the standard error output stream;
  • ConsoleColor ForegroundColor: Gets or sets the font color of the console;
  • Encoding InputEncoding: Read-only property that retrieves the standard input stream;
  • bool KeyAvailable: Read-only property that retrieves a value indicating whether a key press is available in the input stream;
  • int LargestWindowHeight: Read-only property that retrieves the largest possible number of console window rows, based on the current font and screen resolution;
  • int LargestWindowWidth: Read-only property that retrieves the largest possible number of console window columns, based on the current font and screen resolution;
  • bool NumberLock: Read-only property that retrieves a value indicating whether the NUM LOCK key is turned on or turned off;
  • TextWriter Out: Gets the standard output stream;
  • Encoding OutputEncoding: Gets or sets the encoding the console uses to write output;
  • string Title: Gets or sets the title to display in the console title bar;
  • bool TreatControlCAsInput: Gets or sets a value indicating whether the combination of the keys CTRL+C is treated as input or as an interruption catched by the OS;
  • int WindowHeight: Gets or sets the height of the console window area;
  • int WindowLeft: Gets or sets the leftmost position of the console window area relative to the screen buffer;
  • int WindowTop: Gets or sets the top position of the console window area relative to the screen buffer;
  • int WindowWidth: Gets or sets the width of the console window.

Event

  • ConsoleCancelEventHandler CancelKeyPress: Raised when the user press CTRL+C.

Methods

  • void Beep(): Two method overloads that play the sound of a beep just once or with a specified frequency and duration through the console speaker;
  • void Clear(): Clears the console buffer and corresponding console window of display information. If you set a background color to the grid and call the Clear method, the whole console changes its color;
  • void MoveBufferArea: Two method overloads that copy a specified source area of the screen buffer to a specified destination area;
  • Stream OpenStandardError(): Two method overloads that retrieve the standard error stream either total or set to a specified buffer size;
  • Stream OpenStandardInput(): Two method overloads that retrieve the standard input stream either total or set to a specified buffer size;
  • Stream OpenStandardOutput(): Two method overloads that retrieve the standard output stream either total or set to a specified buffer size;
  • int Read(): Reads the next character from the standard input stream. The code execution is stopped until the user press the enter key;
  • ConsoleKeyInfo ReadKey(): Two method overload to obtain the next character or function key pressed by the user. The pressed key is displayed in the console window or not depending on the method overload;
  • string ReadLine(): Reads the next line of characters from the standard input stream;
  • void ResetColor(): Changes the background and font colors to the default;
  • void SetBufferSize(int width, int height): Sets the height and width of the screen buffer area to the specified values;
  • void SetCursorPosition(int left, int top): Sets the position of the cursor;
  • void SetError(TextWriter newError): Sets the System.Console.Error property to the specified System.IO.TextWriter object;
  • void SetIn(TextReader newIn): Sets the System.Console.In property to the specified System.IO.TextReader object;
  • void SetOut(TextWriter newOut): Sets the System.Console.Out property to the specified System.IO.TextWriter object;
  • void SetWindowPosition(int left, int top): Sets the position of the console window relative to the screen buffer;
  • void SetWindowSize(int width, int height): Sets the height and width of the console window to the specified values;
  • void Write(): The 18 overloads for the Write method writes content to the output stream. If more than one call to Write method is done, the content is displayed side by side;
  • void WriteLine(): The 19 overloads for the WriteLine method send content to the output stream adding a line terminator in the end. If more than once call to the WriteLine method is done, each call will result in a different line on the console.

How to…

The following code snippet changes the style of the console window and writes some text to it:

/// Defines the background color to gray.
Console.BackgroundColor = ConsoleColor.Gray;

/// Defines the font color to red.
Console.ForegroundColor = ConsoleColor.Red;

/// Enforce the console window to clear
/// all its content and set the background
/// to the entire console window.
Console.Clear();

/// Displays how many columns may be displayed
/// based on the window and buffer width.
Console.WriteLine(“Maximum of columns that may be displayed: {0}”, Console.LargestWindowWidth);

/// Displays how many rows may be displayed
/// based on the window and buffer height.
Console.WriteLine(“Maximum of rows that may be displayed: {0}”, Console.LargestWindowHeight);

/// Adds an empty row.
Console.WriteLine();

/// Request the user to type number to be sum.
Console.WriteLine(“Type the first number to calculate:”);
string s1 = Console.ReadLine();

int n1 = 0;

/// Tries to parse the typed number.
int.TryParse(s1, out n1);

Console.WriteLine(“Type the second number to calculate:”);
string s2 = Console.ReadLine();

int n2 = 0;

/// Tries to parse the typed number.
int.TryParse(s2, out n2);

/// Displays the result of the sum
Console.WriteLine(“The result of the sum is equals: {0}”, (n1 + n2));

Output

Picture 1 - The code stops when waiting for an user input.

Picture 1 - The code stops when waiting for an user input.

Picture 2 - The code continues after typing the first number and requests the second one.

Picture 2 - The code continues after typing the first number and requests the second one.

Picture 3 - The final output

Picture 3 - The final output


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: