Click here to Skip to main content
15,907,328 members
Articles / Programming Languages / Visual Basic
Article

Console Line Editor in VB.NET 2.0

Rate me:
Please Sign up or sign in to vote.
4.22/5 (10 votes)
12 Mar 2006Public Domain2 min read 74.4K   556   30   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:

VB
' 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, along with any associated source code and files, is licensed under A Public Domain dedication


Written By
Web Developer
United States United States
I am Software Engineer for SCW Consulting in Spokane. I primarly write ASP.NET/C# web applications with SQL Server 2k as a back end.

When not coding, I drive what my friends refer to as "Christine" my 1982 Supra.

Comments and Discussions

 
QuestionPROJECT CODE FOR MY WEBSITE [modified] Pin
PANKAJ TYAGI11-Jun-08 23:50
PANKAJ TYAGI11-Jun-08 23:50 

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.