///<summary>Resets tags to null and hides the given grid. ///If isSelectionMade is true, will set textBox.Text to selected item.</summary> private void CloseAndSetRecommendedContacts(ODGrid grid,bool isSelectionMade=true) { ODtextBox textBox=((ODtextBox)grid?.Tag??null); if(textBox==null) { //Done for a bug from TextBox_LostFocus where textBox was null, could be cuase by the form closing and controls being disposed? return; } textBox.Tag=null; grid.Hide(); if(isSelectionMade) { int index=textBox.Text.LastIndexOf(',');//-1 if not found. if(index==-1) {//The selected email is the first email being placed in our textbox. textBox.Text=string.Join(",",grid.SelectedGridRows.Select(x => ((string)x.Tag)).ToList()); } else{//Adding multiple emails. textBox.Text=textBox.Text.Remove(index+1,textBox.Text.Length-index-1);//Remove filter characters textBox.Text+=string.Join(",",grid.SelectedGridRows.Select(x => ((string)x.Tag)).ToList());//Replace with selected email } } textBox.Focus();//Ensures that auto complete textbox maintains focus after auto complete. textBox.SelectionStart=textBox.Text.Length;//Moves cursor to end of the text in the textbox. }
///<summary>Resets tags to null and hides the given grid. ///If isSelectionMade is true, will set textBox.Text to selected item.</summary> private void CloseAndSetRecommendedContacts(ODGrid grid, bool isSelectionMade = true) { ODtextBox textBox = (ODtextBox)grid.Tag; textBox.Tag = null; grid.Hide(); if (isSelectionMade) { int index = textBox.Text.LastIndexOf(','); //-1 if not found. if (index == -1) //The selected email is the first email being placed in our textbox. { textBox.Text = string.Join(",", grid.SelectedGridRows.Select(x => ((string)x.Tag)).ToList()); } else //Adding multiple emails. { textBox.Text = textBox.Text.Remove(index + 1, textBox.Text.Length - index - 1); //Remove filter characters textBox.Text += string.Join(",", grid.SelectedGridRows.Select(x => ((string)x.Tag)).ToList()); //Replace with selected email } } textBox.Focus(); //Ensures that auto complete textbox maintains focus after auto complete. textBox.SelectionStart = textBox.Text.Length; //Moves cursor to end of the text in the textbox. }
///<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(); }