Click here to Skip to main content
15,868,016 members
Articles / Desktop Programming / Windows Forms

Console Control

Rate me:
Please Sign up or sign in to vote.
4.94/5 (23 votes)
8 Jul 2016CPOL2 min read 45.1K   3.5K   30   16
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)


Written By
Web Developer http://www.icemanind.com
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Questiontips in compiling it in VS 2019 Pin
Southmountain2-May-22 8:23
Southmountain2-May-22 8:23 
SuggestionSmall code changes, to change control size Pin
parmando31-Mar-20 8:44
parmando31-Mar-20 8:44 
GeneralRe: Small code changes, to change control size Pin
Southmountain2-May-22 8:21
Southmountain2-May-22 8:21 
QuestionVery good, thank. Pin
sapwood5-Dec-19 19:21
sapwood5-Dec-19 19:21 
QuestionHow to change the size? Pin
Flux OS9-Aug-18 1:33
Flux OS9-Aug-18 1:33 
GeneralPlease show and explain the code Pin
PIEBALDconsult9-Jul-16 18:20
mvePIEBALDconsult9-Jul-16 18:20 
QuestionAn alternative... Pin
Marc Clifton9-Jul-16 8:36
mvaMarc Clifton9-Jul-16 8:36 
GeneralRe: An alternative... Pin
PIEBALDconsult9-Jul-16 9:31
mvePIEBALDconsult9-Jul-16 9:31 
GeneralRe: An alternative... Pin
Marc Clifton9-Jul-16 10:21
mvaMarc Clifton9-Jul-16 10:21 
PraiseNice Pin
ArpitNagar9-Jul-16 8:16
ArpitNagar9-Jul-16 8:16 
GeneralMy vote of 5 Pin
Franc Morales8-Jul-16 23:44
Franc Morales8-Jul-16 23:44 
PraiseExcellent... Pin
klinkenbecker25-Apr-16 15:07
klinkenbecker25-Apr-16 15:07 
PraiseVery nice Pin
MichaelC#Noob12-Nov-15 6:25
professionalMichaelC#Noob12-Nov-15 6:25 
Questionshell replacement? Pin
John Torjo10-Nov-15 19:49
professionalJohn Torjo10-Nov-15 19:49 
AnswerRe: shell replacement? Pin
icemanind11-Nov-15 5:52
icemanind11-Nov-15 5:52 
I never really thought about it, but I'm intrigued. Let me see what I can do and get back to you!
GeneralRe: shell replacement? Pin
John Torjo11-Nov-15 18:39
professionalJohn Torjo11-Nov-15 18:39 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.