Question

I am a bit confused here:

Windows Forms Application, in C#.

Let's say I have a text box. And when the user enters text into that text box, and submits that to the server, it populates what the user wrote into an internal List<string>.

Now, let's say that when a user presses like ctrl + UP on the keyboard, that I want it to return the LAST message they typed (which is essentially the last message added to the List<string> collection.

And if the user presses ctrl + Down on the keyboard, it should return the previous message that occurred before the last up was pressed.

But, oddly, i'm utterly confused as to how to implement this. LOL i know it's like uber simple, but strangely, i can't seem to figure it out.

inputPublicHistory[inputPublicHistory.Count - 1]

Seems like a logical method to pull the previous history, like when i press 'up'. But, obviously, w/o a method to track where that index is, it's hard to compare.

inputPublicHistoryIndex +=1

I could add that after a the user presses the Ctrl + Up on the keyboard, to increment the index, and just return that result, but is that really a valid/proper way to do this?

Maybe i'm just overthinking this, perhaps one of you awesome people here could help me get my h ead screwed on straight with such a simple task.

Was it helpful?

Solution

I would do pretty much what you have outlined except when you save the user's latest input, set the index tracking where they are to .Count - 1. If they press CTRL+Up and the index is at .Count - 1, show them the last item in the list. If they then press CTRL+Down, decrement the index and show them that item, but don't (obviously) go below 0.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top