private void ApplyFilter() { string text = listFilterText.ToString(); mainControlText.Text = text; // Remove existing highlight highlightedItem?.ModifyStatesView(showHighlight: false); // Find menu items starting with the given filter text var firstMatchedItem = MenuItems.Where(m => m.Text.IndexOf(text, StringComparison.OrdinalIgnoreCase) == 0).FirstOrDefault(); if (firstMatchedItem != null) { // Highlight our new match highlightedItem = firstMatchedItem; highLightedIndex = MenuItems.IndexOf(highlightedItem); highlightedItem.ModifyStatesView(showHighlight: true); // and scroll into view DropDownContainer.ScrollIntoView(highlightedItem); } }
protected override void ShowMenu() { if (this.Parent == null) { return; } base.ShowMenu(); if (selectedIndex >= MenuItems.Count - 1) { selectedIndex = MenuItems.Count - 1; } // Show and highlight the previously selected or the first item if (selectedIndex >= 0) { var selectedMenuItem = MenuItems[selectedIndex]; // Scroll the selected item into view DropDownContainer.ScrollIntoView(selectedMenuItem); // Highlight the selected item highlightedItem = selectedMenuItem; highlightedItem.ModifyStatesView(showHighlight: true); } else if (MenuItems.Count > 0) { highlightedItem = MenuItems[0]; highlightedItem?.ModifyStatesView(showHighlight: true); } listFilterText = new StringBuilder(); DropDownContainer.KeyPressed += (s, e) => { listFilterText.Append(e.KeyChar); ApplyFilter(); }; DropDownContainer.KeyUp += (s, e) => { switch (e.KeyCode) { case Keys.Up: if (highLightedIndex > 0) { highLightedIndex--; } break; case Keys.Down: if (highLightedIndex < this.MenuItems.Count - 1) { highLightedIndex++; } break; } // Remove existing highlightedItem?.ModifyStatesView(showHighlight: false); highlightedItem = this.MenuItems[highLightedIndex]; highlightedItem?.ModifyStatesView(showHighlight: true); // if (!highlightedItem.Visible) // only scroll if clipped { DropDownContainer.ScrollIntoView(highlightedItem); } this.Invalidate(); }; DropDownContainer.KeyDown += (s, keyEvent) => { switch (keyEvent.KeyCode) { case Keys.Escape: listFilterText = new StringBuilder(); DropDownContainer.CloseMenu(); break; case Keys.Enter: if (highlightedItem != null) { SelectedIndex = MenuItems.IndexOf(highlightedItem); DropDownContainer.CloseMenu(); listFilterText = null; } break; case Keys.Back: if (listFilterText != null && listFilterText.Length > 0) { listFilterText.Length -= 1; } keyEvent.Handled = true; keyEvent.SuppressKeyPress = true; ApplyFilter(); break; } }; DropDownContainer.Closed += (s, e) => { mainControlText.Text = selectedIndex == -1 ? this.noSelectionString : MenuItems[SelectedIndex].Text; this.Focus(); }; }