int idx = originalList.IndexOf(selectedItem);
int count = 1;
newList.Add(selectedItem); // first add the selecteditem to the new list
// first try to get two elements before the selected item
int lastBeforeIndx = idx;
for (int i = idx - 1; i >= 0; i--)
{
if (count == 3)
{
break;
}
newList.Insert(0, originalList[i]);
lastBeforeIndx = i;
count++;
}
// then try to get 2 elements to the right of the selected item
for (int i = idx + 1; i <= originalList.Count; i++)
{
if (count == 5)
{
break;
}
newList.Add(originalList[i]);
count++;
}
// all finished. Only want to show five at a time in this example
if (count == 5)
{
return;
}
// if not at 5, try adding to the left again
for (int i = lastBeforeIndx - 1; i >= 0; i--)
{
if (count == 5)
{
break;
}
newList.Insert(0, originalList[i]);
count++;
}
int idx = originalList.IndexOf(selectedItem);
int count = 1;
newList.Add(selectedItem); // first add the selecteditem to the new list
// first try to get two elements before the selected item
int lastBeforeIndx = idx;
for (int i = idx - 1; i >= 0; i--)
{
if (count == 3)
{
break;
}
newList.Insert(0, originalList[i]);
lastBeforeIndx = i;
count++;
}
// then try to get 2 elements to the right of the selected item
for (int i = idx + 1; i <= originalList.Count; i++)
{
if (count == 5)
{
break;
}
newList.Add(originalList[i]);
count++;
}
// all finished. Only want to show five at a time in this example
if (count == 5)
{
return;
}
// if not at 5, try adding to the left again
for (int i = lastBeforeIndx - 1; i >= 0; i--)
{
if (count == 5)
{
break;
}
newList.Insert(0, originalList[i]);
count++;
}