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

Remove already selected item from AutoComplexBox auto complete items

5 Answers 727 Views
AutoCompleteBox
This is a migrated thread and some comments may be shown as answers.
Vannick
Top achievements
Rank 1
Vannick asked on 06 Jan 2018, 09:38 AM

Hi,

Is it possible that each time we select a new item in the autocompletebox, the selected item disappears from the list of auto complete items.

This is to avoid to enter twice the same items.

If this is possible, how to do it?

I put an attachment to show what is my current behaviour. What i would like is that once if have selected tag, it does not appears anymore in the list of choice

FYI, I am using auto completion with unbound mode.

Thanks a lot

 

5 Answers, 1 is accepted

Sort by
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 08 Jan 2018, 11:31 AM
Hello, Vannick,  

Thank you for writing.  

In order to achieve your goal, you can create a derivative of the RadAutoCompleteBoxListElement and override its SuggestOverride method. This is the place where you can manipulate the Items collection that will be displayed in the popup. Here is the sample code snippet:
public static List<string> tokens = new List<string>();
 
public RadForm1()
{
    InitializeComponent();
}
 
private void RadForm1_Load(object sender, EventArgs e)
{
    this.customersTableAdapter.Fill(this.nwindDataSet.Customers);
 
    this.radAutoCompleteBox1.AutoCompleteDataSource = this.customersBindingSource;
    this.radAutoCompleteBox1.AutoCompleteDisplayMember = "ContactName";
    this.radAutoCompleteBox1.AutoCompleteValueMember = "CustomerID";
     
    this.radAutoCompleteBox1.Items.CollectionChanged += Items_CollectionChanged;
}
 
public class CustomRadAutoCompleteBox : RadAutoCompleteBox
{
    public override string ThemeClassName 
    {
        get
        {
            return typeof(RadAutoCompleteBox).FullName; 
        }
    }
 
    protected override RadTextBoxControlElement CreateTextBoxElement()
    {
        return new CustomRadAutoCompleteBoxElement();
    }
}
 
public class CustomRadAutoCompleteBoxElement : RadAutoCompleteBoxElement
{
    protected override Type ThemeEffectiveType    
    {
        get   
        {
            return typeof(RadAutoCompleteBoxElement);    
        }
    }
 
    protected override RadTextBoxListElement CreateListElement()
    {
        return new CustomRadTextBoxListElement();
    }
 
    public class CustomRadTextBoxListElement : RadAutoCompleteBoxListElement
    {
        protected override void SuggestOverride(string pattern)
        {
            base.SuggestOverride(pattern);
            if (pattern != string.Empty && tokens.Count > 0)
            {
                for (int index = 0; index <= this.Items.Count - 1; index++)
                {
                    if (tokens.Contains(this.Items[index].Text))
                    {
                        this.Items[index].Height = 0;
                    }
                    else
                    {
                        this.Items[index].Height = 20;
                    }
                }
            }
        }
    }
}
 
private void Items_CollectionChanged(object sender, Telerik.WinControls.Data.NotifyCollectionChangedEventArgs e)
{
    if (e.Action == Telerik.WinControls.Data.NotifyCollectionChangedAction.Add)
    {
        tokens.Add((e.NewItems[0] as RadTokenizedTextItem).Text);
    }
    else if (e.Action == Telerik.WinControls.Data.NotifyCollectionChangedAction.Remove)
    {
        tokens.Remove((e.NewItems[0] as RadTokenizedTextItem).Text);
    }
}

I hope this information helps. Should you have further questions I would be glad to help.
 
Note that this is just a sample approach and it may not cover all possible cases. Feel free to modify it in a way which suits your requirement best.

Regards,
Dess
Progress Telerik
Try our brand new, jQuery-free Angular components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
0
Ajith
Top achievements
Rank 1
answered on 13 Dec 2018, 05:06 AM
Here we are not having any reference to SuggestOverride(string pattern) how can we call that function so that duplicate could be avoided? In my case Items_CollectionChanged event is only fired when an item is added or deleted?
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 13 Dec 2018, 12:38 PM
Hello, Ajith,     

I have attached my sample project for your reference. When a token is already added it is not shown in the list with suggestion when you start typing.

I hope this information helps. If you need any further assistance please don't hesitate to contact me. 

Regards,
Dess | Tech Support Engineer, Sr.
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
0
Ajith
Top achievements
Rank 1
answered on 23 Jan 2019, 06:31 AM

Hi Dess,

Thanks for your support. I was able to tackle the problem up to some extend, unfortunately I have encountered some issue. The problem is that if selected a specific value from suggestion in auto complete box  i may not be able find the same value after completion of that operation. Is there currently any workarounds for this? Can any one please guide me how to tackle this issue too. Thanks in advance.

0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 23 Jan 2019, 03:07 PM
Hello, Ajith,      

If I understand your concern correctly, you type something in the text box and a suggestions pop up is shown. Then you select an item and a token for it is created. However, if you type the same text again, the suggestions in the popup won't display the already tokenized item. This is expected behavior in the custom implementation because it is how the duplicated entries are avoided. If you need some other behavior, could you please specify in details what behavior you are expecting? Thus, we would be able to think about a suitable solution and assist you further.

I would like to note that we already have a feature request for introducing a mode for disabling the selection of already selected items. You can track its progress, subscribe for status changes and add your comments on the following link - https://feedback.telerik.com/winforms/1372559-add-radautocompletebox-mode-disabling-the-selection-of-already-selected-items
There is also a sample project attached the feedback item.

I am looking forward to your reply.

Regards,
Dess | Tech Support Engineer, Sr.
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Tags
AutoCompleteBox
Asked by
Vannick
Top achievements
Rank 1
Answers by
Dess | Tech Support Engineer, Principal
Telerik team
Ajith
Top achievements
Rank 1
Share this question
or