Navigating a listbox using keyboard chars

2 posts, 0 answers
  1. Sean
    Sean avatar
    3 posts
    Member since:
    Jun 2009

    Posted 25 Jun 2009 Link to this post

    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]; 

    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.
    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.
  2. Victor
    Victor avatar
    1349 posts

    Posted 26 Jun 2009 Link to this post

    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,
    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.
  3. DevCraft banner
Back to Top