From 8f3e912c1c55f2375d79cf8104bbdae8bdf708fd Mon Sep 17 00:00:00 2001 From: Bruce Wayne Date: Wed, 22 Jan 2020 15:10:33 +0800 Subject: [PATCH] Support search mode --- Netch/Forms/MainForm.Designer.cs | 10 +- Netch/Override/SearchComboBox.cs | 152 +++++++++++++++++++++++++++++++ 2 files changed, 158 insertions(+), 4 deletions(-) create mode 100644 Netch/Override/SearchComboBox.cs diff --git a/Netch/Forms/MainForm.Designer.cs b/Netch/Forms/MainForm.Designer.cs index 32f59e75..157d777a 100644 --- a/Netch/Forms/MainForm.Designer.cs +++ b/Netch/Forms/MainForm.Designer.cs @@ -1,4 +1,6 @@ -namespace Netch.Forms +using Netch.Override; + +namespace Netch.Forms { partial class MainForm { @@ -56,7 +58,7 @@ this.DeletePictureBox = new System.Windows.Forms.PictureBox(); this.EditPictureBox = new System.Windows.Forms.PictureBox(); this.ModeLabel = new System.Windows.Forms.Label(); - this.ModeComboBox = new System.Windows.Forms.ComboBox(); + this.ModeComboBox = new System.Windows.Forms.SearchComboBox(); this.ServerComboBox = new System.Windows.Forms.ComboBox(); this.ServerLabel = new System.Windows.Forms.Label(); this.StatusStrip = new System.Windows.Forms.StatusStrip(); @@ -328,7 +330,7 @@ // ModeComboBox // this.ModeComboBox.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawFixed; - this.ModeComboBox.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; + this.ModeComboBox.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDown; this.ModeComboBox.FormattingEnabled = true; this.ModeComboBox.IntegralHeight = false; this.ModeComboBox.Location = new System.Drawing.Point(57, 53); @@ -526,7 +528,7 @@ private System.Windows.Forms.GroupBox ConfigurationGroupBox; private System.Windows.Forms.Label ServerLabel; private System.Windows.Forms.Label ModeLabel; - private System.Windows.Forms.ComboBox ModeComboBox; + private System.Windows.Forms.SearchComboBox ModeComboBox; private System.Windows.Forms.ComboBox ServerComboBox; private System.Windows.Forms.StatusStrip StatusStrip; private System.Windows.Forms.ToolStripStatusLabel StatusLabel; diff --git a/Netch/Override/SearchComboBox.cs b/Netch/Override/SearchComboBox.cs new file mode 100644 index 00000000..267edca1 --- /dev/null +++ b/Netch/Override/SearchComboBox.cs @@ -0,0 +1,152 @@ +using System.Diagnostics; +using System.Linq; + +namespace System.Windows.Forms +{ + [System.ComponentModel.DesignerCategory(@"Code")] + public class SearchComboBox : ComboBox + { + public SearchComboBox() + { + AutoCompleteMode = AutoCompleteMode.Suggest; + } + + protected override void OnTextChanged(EventArgs e) + { + try + { + if (DesignMode || !string.IsNullOrEmpty(Text) || !Visible) return; + + ResetCompletionList(); + } + finally + { + base.OnTextChanged(e); + } + } + + protected override void OnKeyUp(KeyEventArgs e) + { + try + { + if (DesignMode || e.KeyData == Keys.Up || e.KeyData == Keys.Down) return; + + if (e.KeyData == Keys.Return) + { + e.Handled = true; + if (newList.Length > 0 && !newList.Select(o => o.ToString()).Contains(Text)) + { + Text = newList[0].ToString(); + } + + DroppedDown = false; + return; + } + + ReevaluateCompletionList(); + } + finally + { + base.OnKeyUp(e); + } + } + + private void ResetCompletionList() + { + _previousSearchTerm = null; + try + { + SuspendLayout(); + + var originalList = (object[])Tag; + if (originalList == null) + { + Tag = originalList = Items.Cast().ToArray(); + } + + if (Items.Count == originalList.Length) return; + + while (Items.Count > 0) + { + Items.RemoveAt(0); + } + + Items.AddRange(originalList); + } + finally + { + ResumeLayout(true); + } + } + + private string _previousSearchTerm; + private object[] newList; + private void ReevaluateCompletionList() + { + var currentSearchTerm = Text.ToLowerInvariant(); + if (currentSearchTerm == _previousSearchTerm) return; + + _previousSearchTerm = currentSearchTerm; + try + { + SuspendLayout(); + + var originalList = (object[])Tag; + if (originalList == null) + { + Tag = originalList = Items.Cast().ToArray(); + } + + if (string.IsNullOrEmpty(currentSearchTerm)) + { + if (Items.Count == originalList.Length) return; + + newList = originalList; + } + else + { + newList = originalList.Where(x => x.ToString().ToLowerInvariant().Contains(currentSearchTerm)).ToArray(); + } + + try + { + while (Items.Count > 0) + { + Items.RemoveAt(0); + } + } + catch + { + try + { + Items.Clear(); + } + catch + { + // ignored + } + } + + Items.AddRange(newList.ToArray()); + } + finally + { + if (currentSearchTerm.Length >= 2 && !DroppedDown) + { + DroppedDown = true; + Cursor.Current = Cursors.Default; + Text = currentSearchTerm; + Select(currentSearchTerm.Length, 0); + } + + if (Items.Count > 0) + { + DroppedDown = false; + DroppedDown = true; + } + + ResumeLayout(true); + } + } + } +}