ArrayList Items

G

Guest

I have an ArrayList with 5 elements in it. I have a Next and a Prev button
on my form. I need to show the first element in a textbox when the form
opens. I need to move to the next element in the ArrayList when I click the
Next button and move to the previous element (if I am not on the first
element) when clicking the Prev button. Is there a way to keep track of
which element is in the textbox and then show either the next or previous
elements?
 
P

Peter Duniho

I have an ArrayList with 5 elements in it. I have a Next and a Prev
button
on my form. I need to show the first element in a textbox when the form
opens. I need to move to the next element in the ArrayList when I click
the
Next button and move to the previous element (if I am not on the first
element) when clicking the Prev button. Is there a way to keep track of
which element is in the textbox and then show either the next or previous
elements?

Yes.

Seriously though, your question doesn't have anything to do with
ArrayLists, and it has everything to do with how you handle the user
interface. You may want to consider using a more relevant subject in
future posts.

I will assume that the elements in your ArrayList either are strings, or
have a sensible ToString() implementation that you are using to set the
Text property of the TextBox. Then, all you need is an index that is
changed each time you click a button, and update the Text property
accordingly.

For example:

private void buttonNext_Click(object sender, EventArgs e)
{
_iobj = (_iobj + 1) % _list.Count;
textbox.Text = _list[iobj].ToString();
}

Where you have an "int _iobj" member in your class, initialized to 0 (the
first element in the list) and your ArrayList is named "_list", also in
your class.

I leave the implementation of buttonPrev_Click() to you as a homework
exercise.

Pete
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top