How Could I remove the Duplicates in rad Autocompletetextbox

1 posts, 0 answers
  1. Bhushan
    Bhushan avatar
    3 posts
    Member since:
    Jul 2014

    Posted 19 Jul 2014 Link to this post

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using RadautocompleteDemo.Model;
    using Telerik.WinControls.UI;
    using Telerik.WinControls.Data;
    using Telerik.WinControls;
    namespace RadautocompleteDemo
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();



                radAutoCompleteBox1.AutoCompleteDataSource = new Sample().ListGetSuggestions();
                radAutoCompleteBox1.AutoCompleteDisplayMember = "name";
                radAutoCompleteBox1.AutoCompleteValueMember = "id";



            }

            private void button1_Click(object sender, EventArgs e)
            {
                int i = radAutoCompleteBox1.MaxLength;
                int j = radAutoCompleteBox1.TextLength;

            }

            private void Form1_Load(object sender, EventArgs e)
            {
                this.radAutoCompleteBox1.Items.CollectionChanged += new NotifyCollectionChangedEventHandler(Items_CollectionChanged);
            }
            List<Telerik.WinControls.UI.RadTokenizedTextItem> list = new List<RadTokenizedTextItem>();
           // HashSet<Telerik.WinControls.UI.RadTokenizedTextItem> HashSet = new HashSet<RadTokenizedTextItem>();
            void Items_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
            {
                RadTokenizedTextItemCollection items = sender as RadTokenizedTextItemCollection;
                if (e.Action == NotifyCollectionChangedAction.Add)
                {
                    //list.so

                    foreach (RadElement element in this.radAutoCompleteBox1.TextBoxElement.ViewElement.Children)
                    {
                        TokenizedTextBlockElement token = element as TokenizedTextBlockElement;
                        if (token.Item == e.NewItems.SyncRoot)
                        {
                            MessageBox.Show("Item.already prepset");
                           //NotifyCollectionChangedAction.Remove;
                        }
                       
                    }
                    Int32 index = 0;
                    for (int i = 0; i < e.NewItems.Count; i++)
                    {
                        
                            list.Add((e.NewItems[i]) as RadTokenizedTextItem);
                        
                        //bool chekc = HasDuplicates((e.NewItems[i])as RadTokenizedTextItem);
                            list.Sort();
                           
                            while (index < list.Count - 1)
                            {
                                if (list[index] == list[index + 1])
                                    list.RemoveAt(index);
                                else
                                    index++;
                            }
                    }
                 

                }
            }

            private bool HasDuplicates(RadTokenizedTextItem radTokenizedTextItem)
            {
               // List<RadTokenizedTextItem> vals = new List<RadTokenizedTextItem>();
                bool returnValue = true;
                foreach (var s in list)
                {
                   
                    if (list.Contains(radTokenizedTextItem))
                    {
                        returnValue = false;
                        break;
                    }
                   
                }


                return returnValue;
            }






        }
    }


    I am Trying to remove the duplicate values entered by user..if suppose user enters the duplicate values in the radAutocomplete textbox then i just want to give the messagebox that "Duplicate values are not allowed"and after giving this message i just want to remove the duplicate value entered by the user automaticaly..
    I am new to this controls i tried to find out the events but i didnt get any...
    so please give me the solution
    Thaning You in advance
Back to Top