Auto Complete in the Middle of String on RadDropDownList Control

8 posts, 0 answers
  1. T
    T avatar
    2 posts
    Member since:
    Sep 2011

    Posted 28 Oct 2011 Link to this post

    Hi,
    I'm Looking for something like autocomplete in the middle of string.
    It's described on this thread, but for ASP.NET Ajax:
    http://www.telerik.com/help/aspnet-ajax/combobox-usability-filtering.html
    and
    http://www.telerik.com/help/aspnet-ajax/combobox-usability-filtering.html

    So, is there same functionality for WinForms?

    Thanks in  advance!
  2. Peter
    Admin
    Peter avatar
    1148 posts

    Posted 02 Nov 2011 Link to this post

    Hi T,

    Thank you for writing.

    You should implement a custom AutoCompleteSuggestHelper and override the DefaultFilter method.
    I also recommend turning DropDownList's AutoCompleteMode property to Suggest mode only, please refer to the code snipped:

    public class CustomAutoCompleteSuggestHelper : AutoCompleteSuggestHelper
    {
        public CustomAutoCompleteSuggestHelper(RadDropDownListEditorElement element) : base(element)
        {         
        }
       
        protected override bool DefaultFilter(RadListDataItem item)
        {
            return item.Text.ToLower().Contains(this.Filter.ToLower());
        }
       
        public override void AutoComplete(KeyPressEventArgs e)
        {
            base.AutoComplete(e); 
            if (this.DropDownList.Items.Count > 0)
            {
                this.DropDownList.SelectedIndex = this.DropDownList.FindString(this.Filter);
            }
        }
    }
    //assign the new helper to DropDownList
    radDropDownList.AutoCompleteSuggest = New CustomAutoCompleteSuggestHelper(radDropDownList.DropDownListElement);

    I hope this helps. If you need further assistance, feel free to write back. All the best,
    Peter
    the Telerik team

    Q2’11 SP1 of RadControls for WinForms is available for download (see what's new); also available is the Q3'11 Roadmap for Telerik Windows Forms controls.

  3. UI for WinForms is Visual Studio 2017 Ready
  4. Clive
    Clive avatar
    12 posts
    Member since:
    Sep 2011

    Posted 07 Nov 2011 Link to this post

    Hi,

    I was also looking for a solution to this today. Would the solution Peter provided lead to a memory leak?

    In the AutoCompleteMode property, the set method calls dispose on the previous on the helpers. However if you set the AutoCompleteSuggest then that object is never added to the private autoCompleteHelpers field.

    i.e. the default way
    /// <summary>
            /// Specifies the mode for the automatic completion feature used in the DropDownList 
            /// and the TextBox controls.
            /// </summary>
            [Browsable(true), Category(RadDesignCategory.BehaviorCategory)]
            [DefaultValue(System.Windows.Forms.AutoCompleteMode.None), EditorBrowsable(EditorBrowsableState.Always),
            Description("Specifies the mode for the automatic completion feature used in the DropDownList and TextBox controls.")]
            public AutoCompleteMode AutoCompleteMode
            {
                ...
                set
                {
                    ...
                    foreach (BaseAutoComplete helper in this.autoCompleteHelpers)
                    {
                        helper.Dispose();
                    }
      
                    this.autoCompleteHelpers.Clear();
      
                    this.autoCompleteAppend = null;
                    this.autoCompleteSuggest = null;
      
                    if ((this.autoCompleteMode & AutoCompleteMode.Append) != 0)
                    {
                        this.autoCompleteAppend = new AutoCompleteAppendHelper(this);
                        this.autoCompleteHelpers.Add(this.autoCompleteAppend);
                    }
      
                    if ((this.autoCompleteMode & AutoCompleteMode.Suggest) != 0)
                    {
                        this.autoCompleteSuggest = new AutoCompleteSuggestHelper(this);
                        this.autoCompleteHelpers.Add(this.autoCompleteSuggest);
                    }
                }
            }

    My alternative (and there are many now that I think about it... but this is the least change in my opinion) is to change the line

    this.autoCompleteSuggest = new AutoCompleteSuggestHelper(this); 
    to
    this.autoCompleteSuggest = AutoCompleteSuggestHelperFactory(this); 

    and have a new public field (or property.. whatever)

    [RadToolboxItemAttribute(true)]
        public class RadDropDownListElement : PopupEditorElement, ITooltipOwner
        {
            #region Fields
            public System.Func<RadDropDownListElement, AutoCompleteSuggestHelper> AutoCompleteSuggestHelperFactory = (a) => new AutoCompleteSuggestHelper(a);
        ...

    or alternatively, change the AutoCompleteSuggest property?
    public virtual AutoCompleteSuggestHelper AutoCompleteSuggest
    {
        get
        {
            return autoCompleteSuggest;
        }
        set
        {
            autoCompleteSuggest = value;
            autoCompleteHelpers.Add(value);
        }
    }
  5. Peter
    Admin
    Peter avatar
    1148 posts

    Posted 11 Nov 2011 Link to this post

    Hi Clive,

    Thank you for sharing your concerns and ideas with us.

    You can assign AutoSuggestMode to None and this will clear the AutoCompleteHelpers.
    After that you can assign the AutoCompleteSuggest property to your custom AutoComplete helper - this automatically will turn the AutoSuggestMode to Suggest.

    In my sample solution I assign only one new instance of type CustomAutoCompleteHelper - the old AutoComplete Helper instance (if any) will stay in memory, but this is only one and memory should stay constant e.g. memory size will not increase all the time.

    This behavior can be improved by taking your suggestion into consideration.

    I have updated your Telerik points.

    Regards,
    Peter
    the Telerik team

    Q2’11 SP1 of RadControls for WinForms is available for download (see what's new); also available is the Q3'11 Roadmap for Telerik Windows Forms controls.

  6. evald80
    evald80 avatar
    16 posts
    Member since:
    Sep 2009

    Posted 06 Jun 2013 Link to this post

    Hello,
    I have seen that this feature(autocomplete with contains) is implemented in RadDropDowlist however it's not complete as the example in asp.net http://www.telerik.com/help/aspnet-ajax/combobox-usability-filtering.html

    What is missing is the part that changes the text on the dropdrown. Is it possible to have a solution example?
    Thank yuo
  7. Peter
    Admin
    Peter avatar
    1148 posts

    Posted 11 Jun 2013 Link to this post

    Hi,

    Thank you for writing.

    I am not sure that I fully understand your request from the provided information. Perhaps you are looking for auto completion of the text entered, while using the contains filtering mode. If so, this is not supported scenario, and in fact I am not sure how exactly should it behave, since an item might contain the entered char anywhere in its text, and in this case which should be the auto completed part of the item?

    According to the article you provided, you might be looking for highlighting of the text while searching. By default this is not supported, however, you can use the html-like functionality to implement it. Please refer to the attached project.

    Regards,
    Peter
    Telerik
    RadChart for WinForms is obsolete. Now what?
  8. evald80
    evald80 avatar
    16 posts
    Member since:
    Sep 2009

    Posted 11 Jun 2013 Link to this post

    Hi Peter and thank for the reply,
    Your solution is what i'm searching for.

    In these days i have made differents tests and at the end i have solved like the following one which is quite similar to your solution:
    What do you think?
    ps: sorry but it seems that i cant attach a zip file to this discussion so i have to paste the code here

    public class FormatedAutoCompleteSuggestHelper : AutoCompleteSuggestHelper
        {
            public FormatedAutoCompleteSuggestHelper(RadDropDownListElement owner)
                : base(owner)
            {
                this.SuggestMode = SuggestMode.Contains;
                this.DropDownList.ListElement.VisualItemFormatting += DropDownListElement_VisualItemFormatting;
            }
     
     
            private void DropDownListElement_VisualItemFormatting(object sender, VisualItemFormattingEventArgs args)
            {
                String searchedText = this.Filter.ToUpper(); // the searched text
                String itemText = args.VisualItem.Text.ToUpper();
     
                if (!itemText.Contains(searchedText))
                    return;
     
                if (itemText == searchedText)
                {
                    args.VisualItem.Text = String.Format("<html><strong>{0}</strong></html>", itemText);
                    return;
                }
     
                String firstPartNotToBeFormated = itemText.Substring(0, itemText.IndexOf(searchedText));
                int beginFormatingAt = itemText.IndexOf(searchedText) + searchedText.Length;
                String secondPartNotToBeFormated = itemText.Substring(beginFormatingAt, itemText.Length - beginFormatingAt);
                args.VisualItem.Text = String.Format("<html>{0}<strong>{1}</strong>{2}</html>", firstPartNotToBeFormated, searchedText, secondPartNotToBeFormated);
            }
  9. Peter
    Admin
    Peter avatar
    1148 posts

    Posted 14 Jun 2013 Link to this post

    Hello,

    I am happy that you found a solution for this. Thank you for sharing.

    Regards,
    Peter
    Telerik
    RadChart for WinForms is obsolete. Now what?
Back to Top
UI for WinForms is Visual Studio 2017 Ready