Exemplo n.º 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();
        }
Exemplo n.º 2
0
 private void DropDownToggle()
 {
     if (DroppedDown)            //DroppedDown, so we need to collapse.
     {
         if (_doUsePopup)
         {
             _popup.Close();                    //This collapses the popup.  The popup is NOT disposed.
         }
         else
         {
             gridMain.Visible = false;
         }
         FillText();
         if (SelectionChangeCommitted != null)
         {
             SelectionChangeCommitted(this, new EventArgs());
         }
     }
     else             //Not DroppedDown, so we need to DropDown.
     {
         #region Popup setup
         //If the grid rows differ from the items count, refill the grid because something happened to the ArrayList that the control wasn't aware of.
         //For example, If a ComboBoxMulti never sets an item selected, the grid is never filled, even though the ArrayList has items in it.
         //Filling the grid when opening the dropdown matches the UI behavior found in regular combo boxes.
         SynchronizeGrid();
         Control parent = Parent;
         int     x      = this.Location.X;
         int     y      = this.Location.Y;
         while (parent != null && !typeof(Form).IsAssignableFrom(parent.GetType()))
         {
             x     += parent.Location.X;
             y     += parent.Location.Y;
             parent = parent.Parent;
         }
         if (_doUsePopup)
         {
             _popup           = new PopupWindow(gridMain);
             _popup.AutoClose = false;                  //This prevents the Alt key from collapsing the combobox and prevents other events from collapsing as well.
             _popup.Closed   += PopupWindow_Closed;
             _popup.Opened   += PopupWindow_Opened;
             #endregion Popup setup
             _popup.Show(ParentForm, new Point(x, y + this.Height));
         }
         else
         {
             int gridTop = y + this.Height;
             if (gridTop + gridMain.Height >= ParentForm.ClientSize.Height)
             {
                 //The grid does not have enough room to fit on the form.
                 if (y > ParentForm.ClientSize.Height - gridTop)
                 {
                     //If there's more room, put the grid above the combobox.
                     gridTop = y - gridMain.Height;
                     if (gridTop < 0)
                     {
                         //Not enough room, shrink the grid height.
                         gridMain.Height = y;
                         gridTop         = 0;
                     }
                 }
                 else
                 {
                     //If there's not enough room to show the grid below, shrink the grid height.
                     gridMain.Height = ParentForm.ClientSize.Height - gridTop;
                 }
             }
             gridMain.Location = new Point(x, gridTop);
             gridMain.Anchor   = this.Anchor;
             ParentForm.Controls.Add(gridMain);
             gridMain.Visible = true;
             gridMain.BringToFront();
         }
     }
     //The keyboard listener lifetime begins on first dropdown and ends when the control is disposed or parent form is closed.
     if (_threadCheckKeyboard == null)
     {
         //When DroppedDown, will update input flags for auto collapsing.
         _threadCheckKeyboard = new ODThread(100, WorkerThread_KeyboardListener);
         _threadCheckKeyboard.AddExceptionHandler((ex) => ex.DoNothing());
         _threadCheckKeyboard.Start();
     }
 }