示例#1
0
        ///<summary>Creates a list box under given textBox filled with filtered list of recommended emails based on textBox.Text values.
        ///Key is used to navigate list indirectly.</summary>
        private void RecommendedEmailHelper(ODtextBox textBox, Keys key)
        {
            if (_listHistoricContacts.Count == 0)           //No recommendations to show.
            {
                return;
            }
            //The passed in textBox's tag points to the grid of options.
            //The created grid's tag will point to the textBox.
            if (textBox.Tag == null)
            {
                textBox.Tag = new ODGrid()
                {
                    TranslationName = "",
                };
            }
            ODGrid gridContacts = (ODGrid)textBox.Tag;
            //textBox.Text could contain multiple email addresses.
            //We only want to grab the last few characters as the filter string.
            //[email protected],[email protected],emai => "emai" is the filter.
            //When there is no comma, will just use what is currently in the textbox.
            string emailFilter = textBox.Text.ToLower().Split(',').Last();

            if (emailFilter.Length < 2)          //Require at least 2 characters for now.
            {
                gridContacts.Hide();             //Even if not showing .Hide() won't harm anything.
                textBox.Tag = null;              //Reset tag so that initial logic runs again.
                return;
            }
            #region Key navigation and filtering
            switch (key)
            {
            case Keys.Enter:                    //Select currently highlighted recommendation.
                if (gridContacts.Rows.Count == 0)
                {
                    return;
                }
                CloseAndSetRecommendedContacts(gridContacts, true);
                return;

            case Keys.Up:                    //Navigate the recommendations from the textBox indirectly.
                if (gridContacts.Rows.Count == 0)
                {
                    return;
                }
                //gridContacts is multi select. We are navigating 1 row at a time so clear and set the selected index.
                int index = Math.Max(gridContacts.GetSelectedIndex() - 1, 0);
                gridContacts.SetSelected(false);
                gridContacts.SetSelected(new int[] { index }, true);
                gridContacts.ScrollToIndex(index);
                break;

            case Keys.Down:                    //Navigate the recommendations from the textBox indirectly.
                if (gridContacts.Rows.Count == 0)
                {
                    return;
                }
                //gridContacts is multi select. We are navigating 1 row at a time so clear and set the selected index.
                index = Math.Min(gridContacts.GetSelectedIndex() + 1, gridContacts.Rows.Count - 1);
                gridContacts.SetSelected(false);
                gridContacts.SetSelected(new int[] { index }, true);
                gridContacts.ScrollToIndex(index);
                break;

            default:
                #region Filter recommendations
                List <string> listFilteredContacts = _listHistoricContacts.FindAll(x => x.ToLower().Contains(emailFilter.ToLower()));
                if (listFilteredContacts.Count == 0)
                {
                    gridContacts.Hide();                         //No options to show so make sure and hide the list box
                    textBox.Tag = null;                          //Reset tag.
                    return;
                }
                listFilteredContacts.Sort();
                gridContacts.BeginUpdate();
                if (gridContacts.Columns.Count == 0)                       //First time loading.
                {
                    gridContacts.Columns.Add(new ODGridColumn());
                }
                gridContacts.Rows.Clear();
                foreach (string email in listFilteredContacts)
                {
                    ODGridRow row = new ODGridRow(email);
                    row.Tag = email;
                    gridContacts.Rows.Add(row);
                }
                gridContacts.EndUpdate();
                gridContacts.SetSelected(0, true);                       //Force a selection.
                #endregion
                break;
            }
            #endregion
            if (gridContacts.Tag != null)           //Already initialized
            {
                return;
            }
            //When the text box losses focus, we close/hide the grid.
            //TextBox_LostFocus event fires after the EmailAuto_Click event.
            textBox.Leave += TextBox_LostFocus;
            #region Grid Init
            gridContacts.HeaderHeight  = 0;
            gridContacts.SelectionMode = GridSelectionMode.MultiExtended;
            gridContacts.MouseClick   += EmailAuto_Click;
            gridContacts.Tag           = textBox;
            gridContacts.TitleHeight   = 0;
            gridContacts.Parent        = this;
            gridContacts.BringToFront();
            Point menuPosition = textBox.Location;
            menuPosition.X       += 10;
            menuPosition.Y       += textBox.Height - 1;
            gridContacts.Location = menuPosition;
            gridContacts.Width    = (int)(textBox.Width * 0.75);
            gridContacts.SetSelected(0, true);
            #endregion
            gridContacts.Show();
        }