Source Code: Make RadComboBox act like WinForms ComboBox when Style is DropDownList

2 posts, 0 answers
  1. Michael
    Michael avatar
    12 posts
    Member since:
    Jan 2008

    Posted 15 Jan 2010 Link to this post

    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.

    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.

  2. Victor
    Admin
    Victor avatar
    1351 posts

    Posted 19 Jan 2010 Link to this post

    Hi Michael,

    Thank you very much for sharing your code. This will be helpful to those who require this functionality until it is included in RadComboBox. Your Telerik points have been updated for the contribution.

    Greetings,
    Victor
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
  3. UI for WinForms is Visual Studio 2017 Ready
Back to Top