Hello! Tell me how to organize scrolling through a specific call of array elements. Found on open spaces here such code. It works but not as I would like.

private static Func<int, bool> gt(int val) { return (i => i > val); } private static Func<int, bool> lt(int val) { return (i => i < val); } int q; // начало поиска int w = 6; // конец поиска List<int> items = new List<int>(); private void button4_Click(object sender, RoutedEventArgs e) { q = 0; w = 6; listBox.Items.Clear(); items.Clear(); items.Add(1); // Имеется некоторое колл-во элементов items.Add(2); items.Add(3); items.Add(4); items.Add(5); items.Add(6); items.Add(7); items.Add(8); items.Add(9); items.Add(10); items.Add(11); items.Add(12); items.Add(13); List<int> result = new List<int>(items.Where(gt(q)).Where(lt(w))); foreach (int i in result) { listBox.Items.Add(i.ToString()); q++; } } // ЛИСТАЕМ ВПЕРЕД private void button5_Click(object sender, RoutedEventArgs e) { listBox.Items.Clear(); List<int> result = new List<int>(items.Where(gt(q)).Where(lt(w+=5))); foreach (int i in result) { listBox.Items.Add(i.ToString()); q++; } } // ЛИСТАЕМ НАЗАД private void button6_Click(object sender, RoutedEventArgs e) { listBox.Items.Clear(); List<int> result = new List<int>(items.Where(lt(w)).Where(gt(q-=5))); foreach (int i in result) { listBox.Items.Add(i.ToString()); w--; } } 

Although this method works but there is a small problem. After promotion, variables are tuned to the next. scrolling and if at this moment to return the previous page, the variables will be updated first but the data will not change. To go back a step, click on the button twice. Which of course is very annoying.

    1 answer 1

    Pagination can be done next. in the following way:

     //номер страницы int pageIndex = 2; //размер страницы (количество элементов) int pageSize = 5; //нужный переход var page = items.Skip((pageIndex - 1) * pageSize).Take(pageSize); 

    To control forward / backward you need to know the total number of elements

     int count = items.Count; 

    and make sure that the page number is not less than zero and not more than possible number of pages

     int pageCount = count / pageSize; 
    • Something efficiency is somewhat dubious ... - Qwertiy
    • @Qwertiy is quite a working version, for a reasonable collection size in memory. It is clear that for something larger, you need to "page" from the database. - Bulson