Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Console Line Editor in VB.NET 2.0

0.00/5 (No votes)
12 Mar 2006 1  
Play with the new .NET 2.0's Console features, with this old time Line Editor.

Sample Image - LineEditor.gif

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:

' Turn off Control+C tracking, we use the key 

' sequence to end editing for 

Console.TreatControlCAsInput = True

' This loop is the keyboard reading loop. Here we

' read all of the key strokes and process it

Do
    theKey = Console.ReadKey(True)
    Select Case theKey.Key
        Case ConsoleKey.Enter
            ' ...

        Case ConsoleKey.Escape
            ' ... ETC.

        Case ConsoleKey.UpArrow, _
          ConsoleKey.DownArrow, _
          ConsoleKey.F1 To ConsoleKey.F12
            ' Ignore theses keys. To make a real 

            ' app, we need to ignore more keys.

            ' There are probably some dangerous

            ' keys that can get to Case Else

        Case Else
            ' Everything else is echoed and added 

            ' to the buffer

            If Not (theKey.Key = ConsoleKey.C And _
              theKey.Modifiers = _
              ConsoleModifiers.Control) Then
                Console.Write(theKey.KeyChar)
                ' Other processing here

            End If
    End Select
   ' Control+c breaks us out of the loop

   ' We don't handle this in the Select Case

Loop Until theKey.Key = ConsoleKey.C And _
  theKey.Modifiers = ConsoleModifiers.Control
' Turn Control+C tracking back on.

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.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here