Console app, reading lines etc.

  • Thread starter Thread starter Paul
  • Start date Start date
P

Paul

Hi,

In a console app, using VB2008, I'm trying to emulate some of the
functionality available in cmd.exe. Specifically, if in cmd.exe, and you type
"cd <TAB>" it toggles between folder names.

I need to read information that the user types, or, alternatively, if user
hits the TAB key, I want to toggle the text displayed between a list of
strings I have ready in the app.

Any idea of an available framework for this sort of thing. I'm struggling
with my code, getting myself confused by readkey and readline, and handling
backspaces etc.

Regards
Paul
 
Paul said:
Hi,

In a console app, using VB2008, I'm trying to emulate some of the
functionality available in cmd.exe. Specifically, if in cmd.exe, and you
type
"cd <TAB>" it toggles between folder names.

I need to read information that the user types, or, alternatively, if user
hits the TAB key, I want to toggle the text displayed between a list of
strings I have ready in the app.

Any idea of an available framework for this sort of thing. I'm struggling
with my code, getting myself confused by readkey and readline, and
handling
backspaces etc.

Regards
Paul


I believe the only way to do this is to suppress the keys going to the
console when typed, and only utilizing console.write, bypassing console
input. The following code shows an example:

Sub Main()
Dim input As String = "ok"
Dim idx As Integer = 0
Dim words As List(Of String) = New List(Of String)

words.Add("Green")
words.Add("Blue")
words.Add("Volvo")

While (input <> String.Empty)
input = String.Empty
Dim key As ConsoleKeyInfo
Dim flag As Boolean = True
Dim justtabbed As Boolean = False

While flag
' suppress key going to console
key = Console.ReadKey(True)
Select Case key.Key
Case ConsoleKey.Enter
flag = False
justtabbed = False
Case ConsoleKey.Tab
If (justtabbed) Then
' erase previous word
For j As Integer = 1 To words(idx).Length
Console.Write(ControlChars.Back)
Console.Write(" ")
Console.Write(ControlChars.Back)
Next
input = input.Substring(0,
input.Length - words(idx).Length)
End If
idx = idx + 1
If (idx = words.Count) Then idx = 0
Console.Write(words(idx))
justtabbed = True
input = input + words(idx)
Case Else
Console.Write(key.KeyChar)
input = input + key.KeyChar
justtabbed = False
End Select
End While
Console.WriteLine()
Console.WriteLine("-> [" & input & "]")
End While
End Sub

Hopefully this helps.
 
Hi there,

Thanks FT Mike. That's very useful indeed!

Regards
Paul


Family Tree Mike said:
Paul said:
Hi,

In a console app, using VB2008, I'm trying to emulate some of the
functionality available in cmd.exe. Specifically, if in cmd.exe, and you
type
"cd <TAB>" it toggles between folder names.

I need to read information that the user types, or, alternatively, if user
hits the TAB key, I want to toggle the text displayed between a list of
strings I have ready in the app.

Any idea of an available framework for this sort of thing. I'm struggling
with my code, getting myself confused by readkey and readline, and
handling
backspaces etc.

Regards
Paul


I believe the only way to do this is to suppress the keys going to the
console when typed, and only utilizing console.write, bypassing console
input. The following code shows an example:

Sub Main()
Dim input As String = "ok"
Dim idx As Integer = 0
Dim words As List(Of String) = New List(Of String)

words.Add("Green")
words.Add("Blue")
words.Add("Volvo")

While (input <> String.Empty)
input = String.Empty
Dim key As ConsoleKeyInfo
Dim flag As Boolean = True
Dim justtabbed As Boolean = False

While flag
' suppress key going to console
key = Console.ReadKey(True)
Select Case key.Key
Case ConsoleKey.Enter
flag = False
justtabbed = False
Case ConsoleKey.Tab
If (justtabbed) Then
' erase previous word
For j As Integer = 1 To words(idx).Length
Console.Write(ControlChars.Back)
Console.Write(" ")
Console.Write(ControlChars.Back)
Next
input = input.Substring(0,
input.Length - words(idx).Length)
End If
idx = idx + 1
If (idx = words.Count) Then idx = 0
Console.Write(words(idx))
justtabbed = True
input = input + words(idx)
Case Else
Console.Write(key.KeyChar)
input = input + key.KeyChar
justtabbed = False
End Select
End While
Console.WriteLine()
Console.WriteLine("-> [" & input & "]")
End While
End Sub

Hopefully this helps.
 
Hi,

I'm sure there are more ways to make this more robust. I just added the
following case:
Case ConsoleKey.Backspace
If input.Length > 0 Then
input = input.Remove(input.Length - 1, 1)
Console.Write(ControlChars.Back)
Console.Write(" ")
Console.Write(ControlChars.Back)
justtabbed = False
End If

With use I'll probably add more.

Family Tree Mike said:
Paul said:
Hi,

In a console app, using VB2008, I'm trying to emulate some of the
functionality available in cmd.exe. Specifically, if in cmd.exe, and you
type
"cd <TAB>" it toggles between folder names.

I need to read information that the user types, or, alternatively, if user
hits the TAB key, I want to toggle the text displayed between a list of
strings I have ready in the app.

Any idea of an available framework for this sort of thing. I'm struggling
with my code, getting myself confused by readkey and readline, and
handling
backspaces etc.

Regards
Paul


I believe the only way to do this is to suppress the keys going to the
console when typed, and only utilizing console.write, bypassing console
input. The following code shows an example:

Sub Main()
Dim input As String = "ok"
Dim idx As Integer = 0
Dim words As List(Of String) = New List(Of String)

words.Add("Green")
words.Add("Blue")
words.Add("Volvo")

While (input <> String.Empty)
input = String.Empty
Dim key As ConsoleKeyInfo
Dim flag As Boolean = True
Dim justtabbed As Boolean = False

While flag
' suppress key going to console
key = Console.ReadKey(True)
Select Case key.Key
Case ConsoleKey.Enter
flag = False
justtabbed = False
Case ConsoleKey.Tab
If (justtabbed) Then
' erase previous word
For j As Integer = 1 To words(idx).Length
Console.Write(ControlChars.Back)
Console.Write(" ")
Console.Write(ControlChars.Back)
Next
input = input.Substring(0,
input.Length - words(idx).Length)
End If
idx = idx + 1
If (idx = words.Count) Then idx = 0
Console.Write(words(idx))
justtabbed = True
input = input + words(idx)
Case Else
Console.Write(key.KeyChar)
input = input + key.KeyChar
justtabbed = False
End Select
End While
Console.WriteLine()
Console.WriteLine("-> [" & input & "]")
End While
End Sub

Hopefully this helps.
 
Back
Top