Introduction
In .NET 1.x, console applications were extremely limited. You had extremely limited keyboard handling, and limited control over colors and cursor position. .NET 2.0 expanded the Console
class to gain control over the keyboard, text positioning, and colors. These new Console
properties and methods give me almost as much control as I had in the days of QuickBasic; the syntax is more logical than QuickBasic�s syntax.
Just for a kick, I decided to write a line editor as a simple lab to exercise Console
's new features. This program imitates some of the functionality of DOS�s Edlin, with some object orientation.
Background
In .NET 1.x, we were stuck with Console.ReadLine
, we couldn�t see anything until the user pressed Enter.
Key Handling
In this sample, I use Console.ReadKey
to read individual key strokes, and the ConsoleKey
enum to name keys on the keyboard. I also use Console.TreatControlCAsInput
to turn off checking for Ctl+C, so I can have my program behave like Edlin.
Cursor Position
In .NET 1.x, the only control we had over the cursor position is whether we would send a line feed after we wrote a line (use Console.WriteLine
if you want a line feed, and Console.Write
if you don�t).
Using the code
In this program, the only reusable code is EditLine
's edit method, and I would only use it as a starting point.
Points of Interest
Each line in the loaded text file is represented by an EditLine
object and the whole file is a collection of EditLine
objects in an EditLineList
object.
In the EditLine
class, the fancy key processing is in the Edit
method. Here, I read the key stroke with Console.ReadKey
. I use the ConsoleKey
enum to name different keys in my Select Case
structure where I do key processing. This is the Edit
mode:
Console.TreatControlCAsInput = True
Do
theKey = Console.ReadKey(True)
Select Case theKey.Key
Case ConsoleKey.Enter
Case ConsoleKey.Escape
Case ConsoleKey.UpArrow, _
ConsoleKey.DownArrow, _
ConsoleKey.F1 To ConsoleKey.F12
Case Else
If Not (theKey.Key = ConsoleKey.C And _
theKey.Modifiers = _
ConsoleModifiers.Control) Then
Console.Write(theKey.KeyChar)
End If
End Select
Loop Until theKey.Key = ConsoleKey.C And _
theKey.Modifiers = ConsoleModifiers.Control
Console.TreatControlCAsInput = False
Differences between this program and Edlin
This program was written to exercise the Console
object, and I got bored with it before I got around to getting it to do everything that Edlin does (besides, what�s the point). Things that this program doesn�t do:
- Implement Append, Copy, Move, Replace, and Transfer commands.
- Handle lines more than 70 characters long.
- Adequately error check all keyboard input.
- Support putting more than one command on the command line by putting a semi-colon between commands.
On the other hand, I probably have a more object oriented design than the original.
References
History
- 03/14/2006 - Fixed command line error; program couldn't handle filenames in the command line.