This is a migrated thread and some comments may be shown as answers.

AutoCompleteBox, keep not match string

1 Answer 56 Views
AutoCompleteBox
This is a migrated thread and some comments may be shown as answers.
Oscar Emiliano
Top achievements
Rank 1
Oscar Emiliano asked on 12 Jan 2016, 06:56 PM

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

1 Answer, 1 is accepted

Sort by
0
Hristo
Telerik team
answered on 15 Jan 2016, 03:58 PM
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

Tags
AutoCompleteBox
Asked by
Oscar Emiliano
Top achievements
Rank 1
Answers by
Hristo
Telerik team
Share this question
or