Skip to content

Commit

Permalink
Filters in the "Add New Key Mapping" dialog
Browse files Browse the repository at this point in the history
  • Loading branch information
d0vgan committed Feb 15, 2022
1 parent 9f3de05 commit 1c84319
Showing 1 changed file with 140 additions and 5 deletions.
145 changes: 140 additions & 5 deletions SharpKeys/Dialog_KeyItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Collections.Generic;

namespace SharpKeys
{
Expand All @@ -23,6 +24,17 @@ public class Dialog_KeyItem : System.Windows.Forms.Form
private GroupBox groupBox1;
private Panel mainPanel;
private Button btnCancel;
private Label stFilterFrom;
private TextBox edFilterFrom;
private Label stFilterTo;
private TextBox edFilterTo;

private List<string> stringsFrom = new List<string>();
private List<string> stringsTo = new List<string>();
private string prevFilterFrom = "";
private string prevFilterTo = "";


/// <summary>
/// Required designer variable.
/// </summary>
Expand Down Expand Up @@ -70,8 +82,12 @@ private void InitializeComponent()
this.btnOK = new System.Windows.Forms.Button();
this.groupBox2 = new System.Windows.Forms.GroupBox();
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.stFilterFrom = new System.Windows.Forms.Label();
this.edFilterFrom = new System.Windows.Forms.TextBox();
this.mainPanel = new System.Windows.Forms.Panel();
this.btnCancel = new System.Windows.Forms.Button();
this.stFilterTo = new System.Windows.Forms.Label();
this.edFilterTo = new System.Windows.Forms.TextBox();
this.groupBox2.SuspendLayout();
this.groupBox1.SuspendLayout();
this.mainPanel.SuspendLayout();
Expand All @@ -96,7 +112,7 @@ private void InitializeComponent()
this.btnFrom.Margin = new System.Windows.Forms.Padding(6);
this.btnFrom.Name = "btnFrom";
this.btnFrom.Size = new System.Drawing.Size(138, 42);
this.btnFrom.TabIndex = 1;
this.btnFrom.TabIndex = 3;
this.btnFrom.Text = "Type &Key";
this.btnFrom.UseVisualStyleBackColor = false;
this.btnFrom.Click += new System.EventHandler(this.btnFrom_Click);
Expand All @@ -109,7 +125,7 @@ private void InitializeComponent()
this.btnTo.Margin = new System.Windows.Forms.Padding(6);
this.btnTo.Name = "btnTo";
this.btnTo.Size = new System.Drawing.Size(138, 42);
this.btnTo.TabIndex = 0;
this.btnTo.TabIndex = 7;
this.btnTo.Text = "Type K&ey";
this.btnTo.UseVisualStyleBackColor = false;
this.btnTo.Click += new System.EventHandler(this.btnTo_Click);
Expand All @@ -123,7 +139,7 @@ private void InitializeComponent()
this.lbTo.Name = "lbTo";
this.lbTo.ScrollAlwaysVisible = true;
this.lbTo.Size = new System.Drawing.Size(418, 506);
this.lbTo.TabIndex = 1;
this.lbTo.TabIndex = 4;
//
// btnOK
//
Expand All @@ -134,12 +150,14 @@ private void InitializeComponent()
this.btnOK.Margin = new System.Windows.Forms.Padding(6);
this.btnOK.Name = "btnOK";
this.btnOK.Size = new System.Drawing.Size(138, 42);
this.btnOK.TabIndex = 2;
this.btnOK.TabIndex = 8;
this.btnOK.Text = "OK";
this.btnOK.UseVisualStyleBackColor = false;
//
// groupBox2
//
this.groupBox2.Controls.Add(this.edFilterTo);
this.groupBox2.Controls.Add(this.stFilterTo);
this.groupBox2.Controls.Add(this.btnTo);
this.groupBox2.Controls.Add(this.lbTo);
this.groupBox2.Location = new System.Drawing.Point(497, 22);
Expand All @@ -153,6 +171,8 @@ private void InitializeComponent()
//
// groupBox1
//
this.groupBox1.Controls.Add(this.stFilterFrom);
this.groupBox1.Controls.Add(this.edFilterFrom);
this.groupBox1.Controls.Add(this.lbFrom);
this.groupBox1.Controls.Add(this.btnFrom);
this.groupBox1.Location = new System.Drawing.Point(26, 22);
Expand All @@ -164,6 +184,25 @@ private void InitializeComponent()
this.groupBox1.TabStop = false;
this.groupBox1.Text = "&Map this key:";
//
// stFilterFrom
//
this.stFilterFrom.AutoSize = true;
this.stFilterFrom.Location = new System.Drawing.Point(8, 300);
this.stFilterFrom.Name = "stFilterFrom";
this.stFilterFrom.Size = new System.Drawing.Size(32, 13);
this.stFilterFrom.TabIndex = 1;
this.stFilterFrom.Text = "Filter:";
//
// edFilterFrom
//
this.edFilterFrom.Location = new System.Drawing.Point(11, 317);
this.edFilterFrom.Name = "edFilterFrom";
this.edFilterFrom.Size = new System.Drawing.Size(100, 20);
this.edFilterFrom.TabIndex = 2;
this.edFilterFrom.TextChanged += new System.EventHandler(this.edFilterFrom_TextChanged);
this.edFilterFrom.KeyDown += new System.Windows.Forms.KeyEventHandler(this.edFilterFrom_KeyDown);
this.edFilterFrom.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.edFilterFrom_KeyPress);
//
// mainPanel
//
this.mainPanel.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
Expand Down Expand Up @@ -191,10 +230,29 @@ private void InitializeComponent()
this.btnCancel.Margin = new System.Windows.Forms.Padding(6);
this.btnCancel.Name = "btnCancel";
this.btnCancel.Size = new System.Drawing.Size(138, 42);
this.btnCancel.TabIndex = 3;
this.btnCancel.TabIndex = 9;
this.btnCancel.Text = "Cancel";
this.btnCancel.UseVisualStyleBackColor = false;
//
// stFilterTo
//
this.stFilterTo.AutoSize = true;
this.stFilterTo.Location = new System.Drawing.Point(8, 300);
this.stFilterTo.Name = "stFilterTo";
this.stFilterTo.Size = new System.Drawing.Size(32, 13);
this.stFilterTo.TabIndex = 5;
this.stFilterTo.Text = "Filter:";
//
// edFilterTo
//
this.edFilterTo.Location = new System.Drawing.Point(11, 317);
this.edFilterTo.Name = "edFilterTo";
this.edFilterTo.Size = new System.Drawing.Size(100, 20);
this.edFilterTo.TabIndex = 6;
this.edFilterTo.TextChanged += new System.EventHandler(this.edFilterTo_TextChanged);
this.edFilterTo.KeyDown += new System.Windows.Forms.KeyEventHandler(this.edFilterTo_KeyDown);
this.edFilterTo.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.edFilterTo_KeyPress);
//
// Dialog_KeyItem
//
this.AutoScaleDimensions = new System.Drawing.SizeF(11F, 24F);
Expand All @@ -213,10 +271,13 @@ private void InitializeComponent()
this.ShowInTaskbar = false;
this.SizeGripStyle = System.Windows.Forms.SizeGripStyle.Hide;
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
this.Shown += new System.EventHandler(this.Dialog_KeyItem_Shown);
this.Paint += new System.Windows.Forms.PaintEventHandler(this.Dialog_KeyItem_Paint);
this.Resize += new System.EventHandler(this.Dialog_KeyItem_Resize);
this.groupBox2.ResumeLayout(false);
this.groupBox2.PerformLayout();
this.groupBox1.ResumeLayout(false);
this.groupBox1.PerformLayout();
this.mainPanel.ResumeLayout(false);
this.ResumeLayout(false);

Expand Down Expand Up @@ -296,5 +357,79 @@ private void Dialog_KeyItem_Resize(object sender, EventArgs e)
{
this.Invalidate();
}
private void Dialog_KeyItem_Shown(object sender, EventArgs e)
{
foreach (var item in lbFrom.Items)
{
stringsFrom.Add(item.ToString());
}

foreach (var item in lbTo.Items)
{
stringsTo.Add(item.ToString());
}
}

private void edFilterFrom_TextChanged(object sender, EventArgs e)
{
string filter = edFilterFrom.Text.ToUpper();
if (filter == prevFilterFrom)
return;

prevFilterFrom = filter;
lbFrom.Items.Clear();
foreach (var item in stringsFrom)
{
if (item.ToUpper().Contains(filter))
lbFrom.Items.Add(item);
}
}

private void edFilterFrom_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Back && e.Control) // Ctrl+BackSpace
{
edFilterFrom.Text = "";
}
}

private void edFilterFrom_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == '\x7F') // 0x7F corresponds to Ctrl+Backspace. Why? Ask Microsoft...
{
e.KeyChar = '\x00';
}
}

private void edFilterTo_TextChanged(object sender, EventArgs e)
{
string filter = edFilterTo.Text.ToUpper();
if (filter == prevFilterTo)
return;

prevFilterTo = filter;
lbTo.Items.Clear();
foreach (var item in stringsTo)
{
if (item.ToUpper().Contains(filter))
lbTo.Items.Add(item);
}
}

private void edFilterTo_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Back && e.Control) // Ctrl+BackSpace
{
edFilterTo.Text = "";
}
}

private void edFilterTo_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == '\x7F') // 0x7F corresponds to Ctrl+Backspace. Why? Ask Microsoft...
{
e.KeyChar = '\x00';
}
}
}
}

0 comments on commit 1c84319

Please sign in to comment.