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 Telerik.WinControls.UI;

namespace _1147300_AutoCompleteBoxRemoveDuplicates
{
    public partial class RadForm1 : Telerik.WinControls.UI.RadForm
    {
        public static List<string> tokens = new List<string>();

        public RadForm1()
        {
            InitializeComponent();
            this.radAutoCompleteBox1.TokenValidating+=radAutoCompleteBox1_TokenValidating;
        }

        private void radAutoCompleteBox1_TokenValidating(object sender, TokenValidatingEventArgs e)
        {
           // e.IsValidToken = false;
            
        }

        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);
            }
        }
    }
}