This question is locked. New answers and comments are not allowed.
I'm posting a source code solution to force the RadComboBox to act like a Windows Forms ComboBox when the DropDownStyle is DropDownList. This is common functionality that has been missing for far too long. This works with 2009 Q2 SP1. The code I'm supplying is a wrapper class.
Note: I'm using LINQ to build the cache lists so this requires .net 3.5. This can be changed to work with .net 2.0 by replacing the LINQ query with a custom "Get entries starting with 'X'" type of function that iterates through the collection.
Note 2: Updated code to deal with list items changing.
I'm also assuming that you're using this code with a databound combo box where the bound items are not changing. If they are subject to change, make sure that you
I hope this helps out anyone with the same issue.
using System; |
using System.Collections.Generic; |
using System.Linq; |
using Telerik.WinControls; |
using Telerik.WinControls.UI; |
/// <summary> |
/// Wrapper class to make RadComboBox act like a WinForms ComboBox |
/// when DropDownStyle is DropDownList |
/// </summary> |
/// <!--Author: Michael Gerety--> |
public class WorkingDropDownList : RadComboBox |
{ |
private char _lastChar; |
private int _lastIndex; |
private Dictionary<char, List<RadComboBoxItem>> _cache; |
public override string ThemeClassName |
{ |
get |
{ |
return "Telerik.WinControls.UI.RadComboBox"; |
} |
set |
{ |
base.ThemeClassName = value; |
} |
} |
public WorkingDropDownList() |
{ |
KeyPress += ComboBox_KeyPress; |
_cache = new Dictionary<char, List<RadComboBoxItem>>(); |
Items.ItemsChanged += new ItemChangedDelegate(Items_ItemsChanged); |
} |
void Items_ItemsChanged(RadItemCollection changed, RadItem target, ItemsChangeOperation operation) |
{ |
//If the items collection changes, we need to remove the cache for the character affected. |
if(target.Text != null) |
{ |
var changedChar = Char.Parse(target.Text.Substring(0, 1)); |
if(_cache.ContainsKey(changedChar)) |
{ |
_cache.Remove(changedChar); |
} |
} |
} |
void ComboBox_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e) |
{ |
if (DropDownStyle == RadDropDownStyle.DropDownList) |
{ |
if (Char.IsLetter(e.KeyChar)) |
{ |
if (Char.ToUpper(e.KeyChar) != _lastChar) |
{ |
_lastChar = Char.ToUpper(e.KeyChar); |
_lastIndex = 0; |
if (!_cache.ContainsKey(_lastChar)) |
{ |
var list = (from i in Items |
where (i as RadComboBoxItem).Text.ToUpper().StartsWith(_lastChar.ToString()) |
select (RadComboBoxItem)i).ToList(); |
_cache.Add(_lastChar, list); |
} |
} |
if (_lastIndex >= _cache[_lastChar].Count) |
{ |
_lastIndex = 0; |
} |
if (_cache[_lastChar].Count > 0) |
{ |
SelectedItem = _cache[_lastChar][_lastIndex]; |
} |
_lastIndex++; |
} |
} |
} |
} |
Note: I'm using LINQ to build the cache lists so this requires .net 3.5. This can be changed to work with .net 2.0 by replacing the LINQ query with a custom "Get entries starting with 'X'" type of function that iterates through the collection.
Note 2: Updated code to deal with list items changing.
I'm also assuming that you're using this code with a databound combo box where the bound items are not changing. If they are subject to change, make sure that you
I hope this helps out anyone with the same issue.