Last 10 text box inputs

Hi guys. I’ve seen some games that save the last 10 things you typed into the text box, by pressing Up on the keyboard. Does anybody know the best way to do this?

Store what was last entered into an array or queue or whatever, and just grab the previous item each time Up is pressed…

THat’s what I was thinking. I figured that was the simplest way and maybe there was a better way of doing it.

If we’re not using VB6, but .Net we can do some cheap trick like this to make life a little easier:

internal class RollingList <t>: List <t>{

	private Int32 _cap;

	internal RollingList(Int32 maxcapacity) {
		_cap = maxcapacity;
	}

	internal new void Add(T item) {
		base.Add(item);
		if (this.Count() > _cap) base.Remove(this.First());
	}

	internal new void AddRange(IEnumerable <t>collection) {
		foreach (var item in collection) this.Add(item);
	}
}</t></t></t>

Saves you the hassle of dealing with keeping track of stuff. 🙂 Unfortunately, in VB6 the solution would be a little less elegant… I don’t have a copy of VB6 installed right now, so can’t really provide the code required. But you’d basically be shifting your array up one every time you add a new item to it.

Ok so I’ve tried the following (Feel free to use)

' Last Command Memory
Public LastCommand(1 To 10)                        As String
Public CommandArray                                As Byte

When the player presses Enter, it does this…

' Last Command Memory
       If MyText <> vbNullString Then
           LastCommand(10) = LastCommand(9)
           LastCommand(9) = LastCommand(8)
           LastCommand(8) = LastCommand(7)
           LastCommand(7) = LastCommand(6)
           LastCommand(6) = LastCommand(5)
           LastCommand(5) = LastCommand(4)
           LastCommand(4) = LastCommand(3)
           LastCommand(3) = LastCommand(2)
           LastCommand(2) = LastCommand(1)
           LastCommand(1) = MyText
       End If

What I’m having problems with, is that the following doesn’t work. I don’t know how to check if the player presses the Up key.

    If KeyAscii = vbKeyUp Then
       ' Last Command Memory
       If CommandArray = 10 Then
           frmGame.txtCommand.Text = vbNullString
           MyText = frmGame.txtCommand.Text
           Exit Sub
       End If

       CommandArray = CommandArray + 1
       frmGame.txtCommand.Text = LastCommand(CommandArray)
       MyText = frmGame.txtCommand.Text
   End If

Ok I’ve figured it out. I have to use the _KeyDown procedure on the textbox


If anybody is interested in doing this, this is what I did, and it now runs 100% without any errors. You may need to adjust the code to work with later versions of Eclipse.

Private Sub txtCommand_KeyDown(KeyCode As Integer, Shift As Integer)
    If KeyCode = vbKeyUp Then
        ' Last Command Memory
        If CommandArray = 10 Then Exit Sub

        If LastCommand(CommandArray + 1) = vbNullString Then Exit Sub

        CommandArray = CommandArray + 1
        frmGame.txtCommand.Text = LastCommand(CommandArray)
        MyText = frmGame.txtCommand.Text
    End If

    If KeyCode = vbKeyDown Then
        ' Last Command Memory
        If CommandArray <= 1 Then
            frmGame.txtCommand.Text = vbNullString
            MyText = frmGame.txtCommand.Text
            CommandArray = 0
            Exit Sub
        End If

        CommandArray = CommandArray - 1
        frmGame.txtCommand.Text = LastCommand(CommandArray)
        MyText = frmGame.txtCommand.Text
    End If
End Sub
Log in to reply