Re: Console app, reading lines etc.
- From: Paul <vis@xxxxxxxxxxxxx>
- Date: Wed, 4 Mar 2009 04:37:02 -0800
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" wrote:
"Paul" <vis@xxxxxxxxxxxxx> wrote in message.
news:FB6E48D4-5F25-408D-BC67-71D284FC1188@xxxxxxxxxxxxxxxx
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.
--
Mike
- References:
- Console app, reading lines etc.
- From: Paul
- Re: Console app, reading lines etc.
- From: Family Tree Mike
- Console app, reading lines etc.
- Prev by Date: automatic shutdown when compress old files
- Next by Date: Re: ClickOnce local cache with no shortcut in the start menu
- Previous by thread: Re: Console app, reading lines etc.
- Next by thread: ClickOnce local cache with no shortcut in the start menu
- Index(es):
Relevant Pages
|