none
scrolling a listbox from code

    Question

  • I need to scroll a listbox from code.  To my surprise there is nothing like listbox.scroll available.  There is a ScrollIntoview but that doesn't quite do it.  I need to be able to scroll n items.  How is this best done?

    thanks, Gary

    Monday, January 04, 2010 7:50 PM

All replies

  • Gary, if you want to scroll to a particular position of the listbox, you can just set the selectedindex/item of the listbox.

    If, however, you want to scroll through the listbox items, one after another, you can use the below code sample:

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        myDispatcherTimer = new DispatcherTimer();
        myDispatcherTimer.Interval = new TimeSpan(0, 0, 0, 0, 500); // 500 Milliseconds
        myDispatcherTimer.Tick += myDispatcherTimer_Tick;
        myDispatcherTimer.Start();
    }

    void myDispatcherTimer_Tick(object sender, EventArgs e)
    {
        if(counter == AutoScrollListBox.Items.Count)
        {
            myDispatcherTimer.Stop();
            return;
        }
        AutoScrollListBox.SelectedIndex = counter;
        counter++;
    }

    Arun

     

    Monday, January 04, 2010 11:55 PM
  • As usual, somebody beats me to the post as Im writing up my sample. This is a perfect response.

    Tuesday, January 05, 2010 12:02 AM
  • I'll take that as a compliment John.

    Arun

    Tuesday, January 05, 2010 12:40 AM
  • nmarun,

    That is not what I am looking for.  Your code causes a listbox_SelectionChanged event which I do not want to do (I want to do this only when an item is selected).  I'm looking to do exactly what the scrollbar does i.e. scroll the list without causing a selectionchanged event.

    Gary

    Sunday, January 10, 2010 6:00 PM