Merge pull request #239 from HMBSbige/feature0

Support search mode
This commit is contained in:
Connection Refused
2020-01-22 16:33:09 +08:00
committed by GitHub
2 changed files with 158 additions and 4 deletions

View File

@@ -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;

View File

@@ -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<object>().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<object>().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);
}
}
}
}