AutoCompleteBox, keep not match string

2 posts, 0 answers
  1. Oscar
    Oscar avatar
    16 posts
    Member since:
    Sep 2013

    Posted 12 Jan Link to this post

    Hi,

    My scenario is that I have a form to capture the profile of a Person, in that form I am using an AutoCompleteBox to enter the Country. I do have a Country Catalog, so the AutoCompleteBox in profile shows existing elements.

    I would like to allow the user to create countries on the fly when capturing a profile. I mean: if the user types a value that does not exist on the catalog, then when clicking the button to store the profile I want to take the not-match string in AutoCompleteBox and use it first to create a Country and then store that new CountryID when saving the whole profile. And of course, the next time the user require that Country it will be available as an existing element from the catalog of countries.

    But I see that not-match strings are lost once AutoCompleteBox loses focus.

    Is it possible to allow AutoCompleteBox keep that not-match string so I can use it when saving the profile as I explained above?

    Thanks in advance for your support,

    Regards

    Oscar

  2. Hristo
    Admin
    Hristo avatar
    716 posts

    Posted 15 Jan Link to this post

    Hi Oscar,

    Thank you for writing.

    This can be easily achieved by adding a new RadListDataItem to the collection filling your RadAutoCompleBox. You only have to extract the correct string from the editor. Please check my code snippet below: 
    public partial class Form1 : Form
    {
        private RadListDataItemCollection items;
     
        public Form1()
        {
            InitializeComponent();
     
            this.items = this.radAutoCompleteBox1.AutoCompleteItems;
     
            this.items.Add(new RadListDataItem("Joe Smith"));
            this.items.Add(new RadListDataItem("Adam Petersen"));
            this.items.Add(new RadListDataItem("Jack Russel"));
            this.items.Add(new RadListDataItem("Daniel Finger"));
            this.items.Add(new RadListDataItem("Richard Vail"));
            this.items.Add(new RadListDataItem("Sebastian Jonnson"));
            this.items.Add(new RadListDataItem("Lee Cooper"));
            this.items.Add(new RadListDataItem("Kelvin Clain"));
            this.items.Add(new RadListDataItem("Maria Jenson"));
            this.items.Add(new RadListDataItem("Chelsea Maarten"));
            this.items.Add(new RadListDataItem("Jenson Chew"));
            this.items.Add(new RadListDataItem("Martin Williams"));
            this.items.Add(new RadListDataItem("Telerik"));
            this.items.Add(new RadListDataItem("James Stone"));
            this.items.Add(new RadListDataItem("Samuel Jackson"));
        }
     
        private void radButton1_Click(object sender, EventArgs e)
        {
            int lastIndexOfToken = this.radAutoCompleteBox1.Text.LastIndexOf(";");
            string newItem;
            if (lastIndexOfToken > 0)
            {
                newItem = this.radAutoCompleteBox1.Text.Substring(lastIndexOfToken + 1);   
            }
            else
            {
                newItem = this.radAutoCompleteBox1.Text;   
            }
     
            this.items.Add(new RadListDataItem(newItem));
        }
    }

    I am also sending you a short video showing the result on my end.

    I hope this helps. Should you have further questions please do not hesitate to write back.

    Regards,
    Hristo Merdjanov
    Telerik

  3. UI for WinForms is Visual Studio 2017 Ready
Back to Top