Part of an application I'm building in VB has terminal functionality, and I'd like to add functionality to it so it remembers past commands in chronological order, similarly to how a windosw terminal works.
In short I'd like you to be able to press the up arrow when the text area is focused and be able to cycle through a list of commands that were entered previously.
I had two ideas for how to do this:
A combobox that, when you hit enter, reads whatever is in combobox.text, whether that be a newly entered command or an old one that was selected. Then add that command to the items of the combobox so you could scroll up and find it again.
Simply a textbox that, when the up arrow is pressed, cycles through some stored queue and sets the text accordingly. This would requires a second queue to remember the cycled through commands and replace them, correct?
Are there any built-in structures of Visual Basic that would be better for this, or is there some better way of doing it?
Thanks for your help.
It sounds like you are looking for something like a Most Recently Used List.
You idea for the
ComboBox
is probably generally the right thing to do. To do what you suggest with aTextBox
would largely result in...aComboBox
.Considerations:
FooBar
matchfooBar
?ComboBox
as the UI picker, you want to use something that will work as a binding source.Here is a nascent MRU class:
BindingList(Of String)
so that you can bind it to aListbox
orComboBox
.The List management is pretty wasteful under the hood. Each time we insert a new item at
myList(0)
.NET has to shift and jigger the underlying array around. The ideal collection type would be aLinkedList
, but that wont work as a binding source and I dont suspect you will have 1000s of items being stored.Usage:
As things are added to the list they will automatically appear in the list. When the user makes a selection
I used the
Leave
event because they can pick from the list or select an item from the list. You code doesn't need to do anything as far as checking for new vs existing items, theAdd
method does that for you.On the left, I entered 4 items,
Delta
being the last one. Next, I typed inAble
correctly. The class removed the old one and floated the new one to the top as the MRU with the new spelling.Since these mean something to your code, use
thisCmd
in the event for whatever they are. For more complex things, perhaps where what they type is just a key or token for something else, use aBindingList(of TokenItem)
in theMRU
ClassBetter than a queue would be an array. The queue allow only sequential access, and after you dequeue it, the object you dequeue get "lost". If you want save it and have also in future session, you can use a file, such cookie or what I prefere, also because prepare the ground for future expansions, an embedded database, such sqlite or firebird. The second one is an incredible powerful database, that allow if you want to get a server, a powerful server