Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there an easy way to navigate a List<t> object?

I have a WPF form that shows a contact (Name, Address and State).

The GUI is bound to a CurrentContact object and they are stored in a List<Contact>.

I would like to add buttons to the bottom:

+-----+  +-----+  +-----+  +-----+ 
| <<  |  |  <  |  |  >  |  | >>  |
+-----+  +-----+  +-----+  +-----+  

Meaning first, previous, next and last.

Is there an easy control or convention to iterate through the list? Or do I need to store an currentItemIndex and roll my own?

like image 251
Vaccano Avatar asked Jan 26 '10 00:01

Vaccano


1 Answers

Lists provide random access, so you don't need to iterate through them to get from one spot to another. In fact, it's probably inefficient to iterate if the list is very long; imagine you wanted to get to the last record from the first one, for example.

In any case, your four buttons would just be:

  • first: list[0]
  • previous: list[currentIndex - 1]
  • next: list[currentIndex + 1]
  • last: list[list.Count - 1]
like image 115
John Feminella Avatar answered Oct 05 '22 22:10

John Feminella