Search code examples
c#listboxselecteditem

select next item in listbox without having to click it


I have a listbox that contains the names of some picture files loaded from a certain folder. When I click an item in the listbox, it's loaded into a picturebox ( sort of a thumbnail ).

I have 2 buttons Next / Previous that i want to use to navigate through the listbox and the listbox to update it's currently selected file accordingly to the picture shown in the picturebox, but using only these buttons, not having to click on any item.

Thank you !


Solution

  • Try the following :

    Previous (Going 'UP') :

    private void listPrevious(object sender, System.Windows.RoutedEventArgs e)
    {
      if (myListBox.SelectedIndex > 0)
      { 
           myListBox.SelectedIndex = myListBox.SelectedIndex - 1; 
      }
    }
    

    Next (Going 'DOWN') :

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