From bd1ca9fdf2965f0004fbd1aac202a59480f7c97c Mon Sep 17 00:00:00 2001
From: Tim Young <tim.young@lightsys.org>
Date: Fri, 10 Mar 2017 14:18:17 +0300
Subject: [PATCH] Search capbility for puzzles

---
 EduNetworkBuilder/ListBoxWindow.Designer.cs | 13 +++-
 EduNetworkBuilder/ListBoxWindow.cs          | 69 ++++++++++++++++++---
 2 files changed, 73 insertions(+), 9 deletions(-)

diff --git a/EduNetworkBuilder/ListBoxWindow.Designer.cs b/EduNetworkBuilder/ListBoxWindow.Designer.cs
index b816f86..3f423da 100644
--- a/EduNetworkBuilder/ListBoxWindow.Designer.cs
+++ b/EduNetworkBuilder/ListBoxWindow.Designer.cs
@@ -35,6 +35,7 @@
             this.btnAdd = new System.Windows.Forms.Button();
             this.cbLoadPuzzlesAtStart = new System.Windows.Forms.CheckBox();
             this.btnReset = new System.Windows.Forms.Button();
+            this.tbSearchBox = new System.Windows.Forms.TextBox();
             this.SuspendLayout();
             // 
             // lbWindowData
@@ -78,7 +79,7 @@
             | System.Windows.Forms.AnchorStyles.Right)));
             this.lblInstructions.Location = new System.Drawing.Point(12, 195);
             this.lblInstructions.Name = "lblInstructions";
-            this.lblInstructions.Size = new System.Drawing.Size(576, 38);
+            this.lblInstructions.Size = new System.Drawing.Size(469, 38);
             this.lblInstructions.TabIndex = 3;
             this.lblInstructions.Text = "label1";
             // 
@@ -115,12 +116,21 @@
             this.btnReset.UseVisualStyleBackColor = true;
             this.btnReset.Click += new System.EventHandler(this.btnReset_Click);
             // 
+            // tbSearchBox
+            // 
+            this.tbSearchBox.Location = new System.Drawing.Point(487, 195);
+            this.tbSearchBox.Name = "tbSearchBox";
+            this.tbSearchBox.Size = new System.Drawing.Size(100, 22);
+            this.tbSearchBox.TabIndex = 7;
+            this.tbSearchBox.TextChanged += new System.EventHandler(this.tbSearchBox_TextChanged);
+            // 
             // ListBoxWindow
             // 
             this.AcceptButton = this.btnOK;
             this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F);
             this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
             this.ClientSize = new System.Drawing.Size(601, 381);
+            this.Controls.Add(this.tbSearchBox);
             this.Controls.Add(this.cbLoadPuzzlesAtStart);
             this.Controls.Add(this.btnReset);
             this.Controls.Add(this.btnAdd);
@@ -145,5 +155,6 @@
         private System.Windows.Forms.Button btnAdd;
         private System.Windows.Forms.CheckBox cbLoadPuzzlesAtStart;
         private System.Windows.Forms.Button btnReset;
+        private System.Windows.Forms.TextBox tbSearchBox;
     }
 }
\ No newline at end of file
diff --git a/EduNetworkBuilder/ListBoxWindow.cs b/EduNetworkBuilder/ListBoxWindow.cs
index 1676c30..0cf4b23 100644
--- a/EduNetworkBuilder/ListBoxWindow.cs
+++ b/EduNetworkBuilder/ListBoxWindow.cs
@@ -22,6 +22,7 @@ namespace EduNetworkBuilder
         PacketMessage myPacketMessage;
         int EditableCount = -1;
         bool processing = false;
+        string FilterString = "";
 
         /// <summary>
         /// Instantiate a ListBoxWindow for use in choosing a network to load
@@ -39,6 +40,7 @@ namespace EduNetworkBuilder
             lblInstructions.Text = NB.Translate("LBW_LBWFilter");
             panelCheckboxes.SuspendLayout();
             string SelectedTag = GetSelectedTag();
+            tbSearchBox.Visible = true;
             if (SelectedTag == NB.Translate("_All"))
                 Text = Text + NB.Translate("_AllS");
             foreach (string str in NB.GetPuzzleTags())
@@ -70,6 +72,7 @@ namespace EduNetworkBuilder
         {
             InitializeComponent();
             LanguagifyComponents();
+            tbSearchBox.Visible = false;
             if (mode == LBContents.routes)
             {
                 MyMode = LBContents.routes;
@@ -226,19 +229,54 @@ namespace EduNetworkBuilder
                 if (Puzzles == null) return;
                 foreach (string str in Puzzles)
                 {
-                    pi = NB.GetPuzzleInfoFromName(str);
-                    shown_name = pi.PuzzleName;
-                    if (Properties.Settings.Default.ScoreList.Contains(str))
-                        shown_name = "* " + shown_name;
-                    foreach(string tag in pi.PuzzleTags)
+                    if (FilterString == "")
                     {
-                        if(isChecked(tag))
+                        pi = NB.GetPuzzleInfoFromName(str);
+                        shown_name = pi.PuzzleName;
+                        if (Properties.Settings.Default.ScoreList.Contains(str))
+                            shown_name = "* " + shown_name;
+                        foreach (string tag in pi.PuzzleTags)
                         {
+                            if (isChecked(tag))
+                            {
+                                lbWindowData.Items.Add(shown_name);
+                                break;
+                            }
+                        }
+                    }else
+                    {
+                        //We are filtering stuff
+                        pi = NB.GetPuzzleInfoFromName(str);
+                        shown_name = pi.PuzzleName;
+                        bool added = false;
+                        string localFilter = ".*" + FilterString + ".*";
+                        if (Properties.Settings.Default.ScoreList.Contains(str))
+                            shown_name = "* " + shown_name;
+                        if (!added && pi.PuzzleName != null && Regex.IsMatch(pi.PuzzleName, localFilter, RegexOptions.IgnoreCase))
+                        {
+                            added = true;
                             lbWindowData.Items.Add(shown_name);
-                            break;
+                        }
+                        if (!added && pi.PuzzleDescription!= null && Regex.IsMatch(pi.PuzzleDescription, localFilter, RegexOptions.IgnoreCase))
+                        {
+                            added = true;
+                            lbWindowData.Items.Add(shown_name);
+                        }
+                        if (!added && pi.PuzzleTitle != null && Regex.IsMatch(pi.PuzzleTitle, localFilter,RegexOptions.IgnoreCase))
+                        {
+                            added = true;
+                            lbWindowData.Items.Add(shown_name);
+                        }
+                        foreach (string tag in pi.PuzzleTags)
+                        {
+                            if (!added && pi.PuzzleTitle != null && Regex.IsMatch(tag, localFilter, RegexOptions.IgnoreCase))
+                            {
+                                lbWindowData.Items.Add(shown_name);
+                                break;
+                            }
                         }
                     }
-               }
+                }
                 if(selected != null && selected != "")
                 {
                     if (lbWindowData.Items.Contains(selected))
@@ -472,5 +510,20 @@ namespace EduNetworkBuilder
             }
         }
 
+        private void tbSearchBox_TextChanged(object sender, EventArgs e)
+        {
+            //If there are more than 2 characters, then we start to see if we can filter it
+            if (tbSearchBox.Text.Length > 2)
+            {
+                //Now we filter the results
+                FilterString = tbSearchBox.Text;
+                UpdateForm();
+            }
+            else
+            {
+                FilterString = "";
+                UpdateForm();
+            }
+        }
     }
 }