views:

23

answers:

2

how i can select five items in the single click on the list box?? if i click any item, i just want to +2 and -2 from the selected index. so my single click need to select five items in the listview. Am using C#(WPF).

+1  A: 

I am not sure what you want to do exactly, but trying. =)

Have a look at the Click event of the ListBox. You can do anything in there, including selecting five items of your choice. You can do it like this (untested, but gives you an idea):

int sindex = listBox1.SelectedIndex;
listBox1.SelectedItems.Clear();
for(int i = Math.Max(sindex - 2, 0); i < Math.Min(sindex + 2, listBox1.Items.Count()), i++)
{
    listBox1.SelectedItems.Add(listBox1.Items[i]);
}

Another thing would be setting the SelectionMode to Multiple or Extended. Does this result in the behaviour you are looking for?

Jens
@Jens i check with the multiple and extend, it allows me to select multiple items, but its not like i said, if i click any items, i just want to +2 and -2 from the selected index. so my single click need to select five items in the listview.. i think u can understand me now?? do u have any idea???
deep
@deep: See my edited answer.
Jens
A: 

have a look at selectionchanged event, and get the index of the selected item and make it +2 and -2 i tried it like this and it works:

void list_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    int idx = list.SelectedIndex;
    int startIdx = idx - 2;
    int endIdx = idx + 2;
    if (startIdx < 0)
    {
        startIdx = 0;
    }
    if (endIdx >= list.Items.Count)
    {
        endIdx = list.Items.Count-1;
    }

    for (int i = startIdx; i <= endIdx; i++)
    {
        if (i != idx)
        {
            list.SelectedItems.Add(list.Items[i]);
        }
    }
}

one problem with this code is you can still use ctrl to select another item so it makes the selecteditems count increased

dnr3