Spen D
Spen D

Reputation: 4345

mulit select listbox in wpf

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).

Upvotes: 1

Views: 877

Answers (2)

Jens
Jens

Reputation: 25563

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?

Upvotes: 1

dnr3
dnr3

Reputation: 1531

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

Upvotes: 0

Related Questions