4

I'm trying to program two buttons to imitate the up/down arrow key behavior, meaning that when I press the button for up, it moves up one item in my listbox and so on. I wrote the following code:

private void mainlistup(object sender, System.Windows.RoutedEventArgs e)
{
    if (listBox_Copy.SelectedIndex != -1 &&
        listBox_Copy.SelectedIndex < listBox_Copy.Items.Count &&
        listBox_Copy.SelectedIndex !=1)
    {
        listBox_Copy.SelectedIndex = listBox_Copy.SelectedIndex - 1;
    }
}

private void mainlistdown(object sender, System.Windows.RoutedEventArgs e)
{
    if (listBox_Copy.SelectedIndex < listBox_Copy.Items.Count &&
       listBox_Copy.SelectedIndex != -1)
    {
        listBox_Copy.SelectedIndex = listBox_Copy.SelectedIndex + 1;
    }
}

This works, however, when pressing the button the item loses its selection... The selection index is set properly (other databinded items, binded to selected item show the correct values) but the listbox item isn't highlighted anymore. How do I set the selected item to become highlighted?

Adi Lester
  • 24,731
  • 12
  • 95
  • 110
internetmw
  • 689
  • 5
  • 13
  • 33

2 Answers2

5

Your ListBox has probably just lost focus. Just do the following after setting the SelectedIndex:

listBox_Copy.Focus();
djdd87
  • 67,346
  • 27
  • 156
  • 195
  • Thanks! Do you happen to know how I can get the listbox to scroll along if an item is selected which is out of view? – internetmw Jul 28 '10 at 13:44
  • @internetwjm: You're probably looking for `ListBox.TopIndex`. See here for details: http://msdn.microsoft.com/en-us/library/system.windows.forms.listbox.topindex.aspx – Hans Olsson Jul 28 '10 at 13:46
  • System.Windows.Controls.Listbox does not contain a definition for topindex...? Do I have to include some sort of reference? – internetmw Jul 28 '10 at 13:58
  • From a quick Google, `BringIntoView` might be what you need, but I've not used WPF, so might be wrong. – djdd87 Jul 28 '10 at 13:59
  • Thanks searched for some time, seems listBox_Copy.ScrollIntoView(listBox_Copy.SelectedItem); is the solution! – internetmw Jul 28 '10 at 14:17
2

As GenericTypeTea says, it sounds likely that it's to do with lost focus. Another issue however is that your code is overcomplicated and won't let you go up to the item at the top. I'd suggest changing it to something like:

Move up

if (listBox_Copy.SelectedIndex > 0)
{ 
     listBox_Copy.SelectedIndex = listBox_Copy.SelectedIndex - 1; 
}

Move down

if (listBox_Copy.SelectedIndex < listBox_Copy.Items.Count - 1)
{
     listBox_Copy.SelectedIndex = listBox_Copy.SelectedIndex + 1;
}            
Hans Olsson
  • 54,199
  • 15
  • 94
  • 116
  • Yeah that's better. only added if(listbox_copy.selectedindex == 0){listbox_copy.focus();} because if first item is selected and I press up it removes focus again.. – internetmw Jul 28 '10 at 13:48
  • Personally, I wouldn't bother encapsulating the Focus() method within an if statement. I'd just do it on every button click for up/down. – djdd87 Jul 28 '10 at 14:03