views:

1114

answers:

1

Hello,

I have TextBox and ListBox with bunch of elements.

TextBox has KeyDown event handler, the idea behind this is to allow user to press up and down keys to scroll inside ListBox while focus is on TextBox.

When user presses "down key" several times, selected element becomes last visible element on screen. If user has reached bottom of visible list element on screen, I want him to see next element after selected element as well.

Please have a look at this 2 screenshots:

Current: http://dl.getdropbox.com/u/204110/current.PNG

Goal: http://dl.getdropbox.com/u/204110/goal.PNG

Thank You

+4  A: 

Look at the ScrollIntoView method on the listbox. You can use this to ensure that the next element to the selected one is always visible.

On down arrow press:

if (listbox.SelectedIndex < listbox.Items.Count - 1)
    listbox.ScrollIntoView(listbox.Items[listbox.SelectedIndex + 1]);

On up arrow press:

if (listbox.SelectedIndex > 0)
    listbox.ScrollIntoView(listbox.Items[listbox.SelectedIndex - 1]);
Martin Harris
Wow that was quick. Thank You!
Daniil Harik