Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / desktop / WinForms

Console Control

4.94/5 (23 votes)
8 Jul 2016CPOL2 min read 47.8K   3.6K  
A Winforms control that looks like a console screen, with full reading and writing support.

Image 1

Introduction

Recently, I was working on a project where I needed to create a WinForms application, but I needed a sort of "Console Window". Not finding anything I liked, I decided to write my own. The console control presented here is built completely from scratch, without using any existing controls and it does all its own rendering.

Using the Code

To use the control, simply reference the DLL or project from your own project. After referencing it, you should be able to see it in the designer window. Simply drag an instance out to your form. You can set the following properties:

  • ShowCursor - True/False. If true, the cursor will be displayed on the console window. If false, then no cursor will be displayed.
  • CursorType - Enumeration. If set to Underline, then the cursor will be rendered as an underline. If set to Block, then the cursor will be rendered as a block. If set to Invisible, then the cursor will be invisible.
  • AllowInput - True/False. If set to true, then the console window will capture keystrokes and emit the LineEntered event (see below).
  • EchoInput - True/False. If set to true, then anytime a key is pressed, the console window will echo the key pressed to the console window.
  • ConsoleBackgroundColor - Color. This property determines the background color of the console window. The default is black.
  • ConsoleForegroundColor - Color. This property determines the foreground color of the console window. The default is gray.
  • CurrentBackgroundColor - Color. This property determines the background color of any text that's printed out to the console window.
  • CurrentForegroundColor - Color. This property determines the foreground color of any text that's printed out to the console window.

The following methods are available:

  • SetCursorPosition() - Sets the row and column of the cursor.
  • GetCursorPosition() - Returns the row and column of the cursor.
  • Write() - Writes a string or character or newline at the current cursor position, using the current background and foreground color or you can specify an implicit foreground and background color.
  • ScrollUp() - Scrolls the console window up 1 or more lines.
  • Clear() - Clears the console window completely and places the cursor in the upper left corner.
  • SetBackgroundColorAt() - Sets the background color at the specified location to the specified color.
  • SetForegroundColorAt() - Sets the foreground color at the specified location to the specified color.
  • SetCharacterAt() - Places a character at the specified location.

History

11/10/2015 - Initial Release

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)