This is a migrated thread and some comments may be shown as answers.

Navigating a listbox using keyboard chars

1 Answer 75 Views
ComboBox and ListBox (obsolete as of Q2 2010)
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Sean
Top achievements
Rank 1
Sean asked on 26 Jun 2009, 02:14 AM
Hi, I'm relatively new to using Telerik controls.
I've built a listbox containing usernames. I want to navigate the listbox using the letters on the keyboard as well as the currently supported directional keys.
I've implemented a onkeypress listener to execute the following
RadListBoxItem docQueue = this.lstDocQueue.FindItem(letter.ToString()); 
this.lstDocQueue.SelectedItem = docQueue.Text; 
RadListBoxElement element = (RadListBoxElement)this.lstDocQueue.RootElement.Children[0]; 
element.ScrollElementIntoView(docQueue); 

It scrolls and selects the RadListBoxItem I want, but if I hit the up or down key, it moves to the next/prev index of the prev RadListBoxItem and not the current one.
Example:
I have a listbox consisting of Adam, Adrian, James, John
Original selection is Adam.
If I hit J, it scrolls to James which is expected.
If I hit down, it highlights Adrian and not John, which is incorrect.

If you can point me in the right direction that would be very helpful. Thanks.

1 Answer, 1 is accepted

Sort by
0
Victor
Telerik team
answered on 26 Jun 2009, 06:42 PM
Hello Sean,

RadListBox has two important items. A selected item (or items, if multi-select is enabled) and an active item. This differentiation is necessary because you may have many selected items but only one of them is active (for example if you have many selected items but you are editing the value of one of them). The arrow key navigation works with the active item, so when you set the selected item, the active item stays different and pressing up or down works according to it.
 
Currently RadListBoxElement has a SetActiveItem() method which is private.

You can do a rather nasty hack and obtain a reference to the method through reflection and call it. This will solve your issue for now. For example:
 
MethodInfo setActiveItem = this.radListBox1.ListBoxElement.GetType().GetMethod("SetActiveItem");  
setActiveItem.Invoke(this.radListBox1.ListBoxElement, new object[] {item}); 

We will make this method public in next week's Q2 2009 release. Setting the SelectedItem or SelectedIndex will be modified to call SetActiveItem() so that the logic will work correctly.

Thank you for reporting this issue and write back if you have other questions.
 

Best wishes,
Victor
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
Tags
ComboBox and ListBox (obsolete as of Q2 2010)
Asked by
Sean
Top achievements
Rank 1
Answers by
Victor
Telerik team
Share this question
or