/// <summary>
        /// Attach the helper to a textbox
        /// </summary>
        /// <param name="textBox"></param>
        /// <param name="enableDeselectOnEsc"></param>
        /// <param name="enableShowListOnRightclick"></param>
        /// <param name="enableTooltip"></param>
        /// <param name="columnHeaders"></param>
        /// <param name="items"></param>
        /// <param name="viewMode"></param>
        /// <param name="postSetFunc"></param>
        public static void AttachTextBox(TextBox textBox, bool enableDeselectOnEsc, bool enableShowListOnRightclick, bool enableTooltip, string[] columnHeaders, EnhancedTextBoxHelper.Items[] items, EnhancedTextBoxHelperList.ViewMode viewMode, Action postSetFunc)
        {
            CleanLists();

            if (textBox == null)
                return;

            UpdateItems(textBox, items, true);

            if (enableDeselectOnEsc)
                SetDeselectOnEsc(textBox);

            if (enableShowListOnRightclick && columnHeaders != null)
                SetShowListOnRightClick(textBox, columnHeaders, viewMode, postSetFunc, true);

            if (enableTooltip)
                SetToolTipOnMouseOver(textBox);
        }
        /// <summary>
        /// Show a tooltip
        /// </summary>
        /// <param name="textBox"></param>
        /// <param name="items"></param>
        private static void ShowTooltip(TextBox textBox, EnhancedTextBoxHelper.Items[] items)
        {
            if (textBox != null)
            {
                if (!String.IsNullOrEmpty(textBox.Text))
                {
                    EnhancedTextBoxHelper.Items item = items.FirstOrDefault(r => r._value == textBox.Text);
                    if (item != null)
                    {
                        if (item._values != null)
                        {
                            string tooltipText = "";
                            foreach (string value in item._values)
                                tooltipText += value + " ";

                            if (textBox.Visible && tooltip.Tag == null)
                            {
                                if (!tooltipShown)
                                {
                                    tooltip.Show(tooltipText, textBox, textBox.Width / 2, textBox.Height / 2);
                                    tooltip.Tag = textBox;
                                    tooltipShown = true;
                                }
                                else
                                {
                                    tooltip.Hide(textBox);
                                    tooltip.Tag = null;
                                    tooltipShown = false;
                                }
                            }
                        }
                    }
                }
            }
        }
        /// <summary>
        /// Update items
        /// </summary>
        /// <param name="textBox"></param>
        /// <param name="items"></param>
        /// <param name="updateAutoComplete"></param>
        public static void UpdateItems(TextBox textBox, EnhancedTextBoxHelper.Items[] items, bool updateAutoComplete)
        {
            if (textBox == null)
                return;

            //clean attached list
            List<TextBox> textBoxToRemove = new List<TextBox>();
            foreach (KeyValuePair<TextBox, EnhancedTextBoxHelper.Items[]> entry in _attached_TextBox)
            {
                if (entry.Key.IsDisposed)
                    textBoxToRemove.Add(entry.Key);
            }
            foreach (TextBox key in textBoxToRemove)
                _attached_TextBox.Remove(key);

            if (!_attached_TextBox.ContainsKey(textBox))
                _attached_TextBox.Add(textBox, items);
            else
                _attached_TextBox[textBox] = items;

            if (updateAutoComplete)
                UpdateAutoComplete(textBox, items);
        }
 /// <summary>
 /// Update items and set/update autocomplete
 /// </summary>
 /// <param name="textBox"></param>
 /// <param name="items"></param>
 public static void UpdateItems(TextBox textBox, EnhancedTextBoxHelper.Items[] items)
 {
     UpdateItems(textBox, items, true);
 }
 /// <summary>
 /// Set/Update AutoComplete on textbox
 /// </summary>
 /// <param name="textBox"></param>
 /// <param name="items"></param>
 public static void UpdateAutoComplete(TextBox textBox, EnhancedTextBoxHelper.Items[] items)
 {
     if (textBox != null && items != null)
     {
         AutoCompleteStringCollection autoCompleteCustomSource = new AutoCompleteStringCollection();
         foreach (EnhancedTextBoxHelper.Items item in items)
             autoCompleteCustomSource.Add(item._value);
         textBox.AutoCompleteMode = AutoCompleteMode.Suggest;
         textBox.AutoCompleteSource = AutoCompleteSource.CustomSource;
         textBox.AutoCompleteCustomSource = autoCompleteCustomSource;
     }
 }
 /// <summary>
 /// Attach the helper to a textbox, view mode set caller on change, deselect enabled, showlist enabled, tooltip enabled
 /// </summary>
 /// <param name="textBox"></param>
 /// <param name="columnHeaders"></param>
 /// <param name="items"></param>
 public static void AttachTextBox(TextBox textBox, string[] columnHeaders, EnhancedTextBoxHelper.Items[] items)
 {
     AttachTextBox(textBox, true, true, true, columnHeaders, items, EnhancedTextBoxHelperList.ViewMode.SelectOnChange, null);
 }
 /// <summary>
 /// Attach the helper to a textbox, set the view mode, deselect enabled, showlist enabled, tooltip enabled
 /// </summary>
 /// <param name="textBox"></param>
 /// <param name="columnHeaders"></param>
 /// <param name="items"></param>
 /// <param name="viewMode"></param>
 /// <param name="postSetFunc"></param>
 public static void AttachTextBox(TextBox textBox, string[] columnHeaders, EnhancedTextBoxHelper.Items[] items, EnhancedTextBoxHelperList.ViewMode viewMode, Action postSetFunc)
 {
     AttachTextBox(textBox, true, true, true, columnHeaders, items, viewMode, postSetFunc);
 }
 /// <summary>
 /// Attach the helper to a textbox, view mode set caller on change
 /// </summary>
 /// <param name="textBox"></param>
 /// <param name="enableDeselectOnEsc"></param>
 /// <param name="enableShowListOnRightclick"></param>
 /// <param name="enableTooltip"></param>
 /// <param name="columnHeaders"></param>
 /// <param name="items"></param>
 public static void AttachTextBox(TextBox textBox, bool enableDeselectOnEsc, bool enableShowListOnRightclick, bool enableTooltip, string[] columnHeaders, EnhancedTextBoxHelper.Items[] items)
 {
     AttachTextBox(textBox, enableDeselectOnEsc, enableShowListOnRightclick, enableTooltip, columnHeaders, items, EnhancedTextBoxHelperList.ViewMode.SelectOnChange, null);
 }
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="textBox"></param>
        /// <param name="columnHeaders"></param>
        /// <param name="items"></param>
        /// <param name="selectedValue"></param>
        /// <param name="viewMode"></param>
        /// <param name="postSetFunc"></param>
        public EnhancedTextBoxHelperList(TextBox textBox, string[] columnHeaders, EnhancedTextBoxHelper.Items[] items, object selectedValue, ViewMode viewMode, Action postSetFunc)
        {
            InitializeComponent();

            this._columnHeaders = columnHeaders;
            this._items = items;
            this._selectedId = selectedValue;
            this._textBox = textBox;
            this._viewMode = viewMode;
            this._postSetFunc = postSetFunc;

            //set title
            if (_viewMode == ViewMode.View)
                this.Text = "View";
            else
                this.Text = "Select";

            //set caller on change
            if (_viewMode == ViewMode.View)
            {
                checkBox_setcalleronchange.Checked = false;
                checkBox_setcalleronchange.Enabled = false;
            }
            else if (_viewMode == ViewMode.SelectOnChange)
            {
                checkBox_setcalleronchange.Checked = true;
            }
            else if (_viewMode == ViewMode.SelectOnDoubleClick)
            {
                checkBox_setcalleronchange.Checked = false;
            }

            //set change referred item mode
            _bindingSource.CurrentChanged += _bindingSource_CurrentChanged;

            //check items consistency
            int valuescount = 0;
            if (items != null && _items.Count() > 0)
            {
                //validate values
                valuescount = items[0]._values.Count();
                foreach (EnhancedTextBoxHelper.Items item in _items)
                {
                    if (item._values.Count() != valuescount)
                    {
                        _items = null;
                        break;
                    }
                }
            }

            //check column headers consistency
            if (_items != null)
            {
                //get max values
                int maxvaluescount = 0;
                foreach (EnhancedTextBoxHelper.Items item in _items)
                {
                    if (item._values != null)
                    {
                        if (item._values.Count() > maxvaluescount)
                        {
                            maxvaluescount = item._values.Count();
                        }
                    }
                }
                //set columns header if null
                if (_columnHeaders == null || _columnHeaders.Length != maxvaluescount)
                {
                    _columnHeaders = new string[] { };
                    for (int i = 1; i <= maxvaluescount; i++)
                        _columnHeaders = _columnHeaders.Concat(new string[] { (i).ToString() }).ToArray();
                }
                //check duplicates
                Dictionary<string, int> columnHeadersCounts = new Dictionary<string, int>();
                foreach (string columnHeader in _columnHeaders)
                {
                    if (columnHeadersCounts.ContainsKey(columnHeader))
                        columnHeadersCounts[columnHeader]++;
                    else
                        columnHeadersCounts.Add(columnHeader, 1);
                }
                List<string> output = new List<string>();
                foreach (KeyValuePair<string, int> pair in columnHeadersCounts)
                {
                    if (pair.Value > 1)
                    {
                        for (int i = 1; i <= pair.Value; i++)
                            output.Add(pair.Key + "-" + i);
                    }
                    else
                        output.Add(pair.Key);
                }
                _columnHeaders = output.ToArray();
            }

            //set filter search hanlder
            advancedDataGridViewSearchToolBar_main.Search += new Zuby.ADGV.AdvancedDataGridViewSearchToolBarSearchEventHandler(advancedDataGridViewSearchToolBar_main_Search);

            //set binding source
            advancedDataGridView_main.DataSource = _bindingSource;
        }