title: Sort search results from text file into listbox
body: <p>what i have is a textbox into which the user types a string. The string will look something like this:
  <code>G32:04:20:40</code></p>
  
  <p>Then the user hits the search button. The program must then open a textfile and search for the "nearest" five strings to the one they entered and display them in a listbox.</p>
  
  <p>I'll define "nearest string" as much as i can (most likely using a very long complicated example).</p>
  
  <p>The data contained in the text file looks like this:</p>
  
  <pre><code>G32:63:58:11 JG01
  G32:86:98:30 JG01
  G33:50:05:11 JG06
  G33:03:84:12 JG05
  G34:45:58:11 JG07
  G35:45:20:41 JG01
  G35:58:20:21 JG03
  </code></pre>
  
  <p>So if the user types in the string :</p>
  
  <p><code>G33:89:03:20</code></p>
  
  <p>The five closest results should display in the list box like this:</p>
  
  <pre><code>G33:50:05:11 JG06
  G33:03:84:12 JG05
  G32:86:98:30 JG01
  G32:63:58:11 JG01
  G34:45:58:11 JG07
  </code></pre>
  
  <p>I should probably point out at this point that strings are coordinates and the value after "JG" represents the value of something at that coordinate.</p>
  
  <p>The way i got to those 5 is by going through the string piece by piece. So the user typed in "G33" so i find all those with G33 at the beginning - if there are none then i find the closest to G33. Then it was "89" so i find all those where the next part is "89" if there are none, then the closest to 89 the better and so on.</p>
  
  <p>What i need to know is how do i go about doing this? I've built the visual components and i also have code in place that deals with similar kinds of things but when it comes to this i'm truely stumped. As you can probably tell by now, i'm rather new to C#, but i'm learning :)</p>
  
  <p>EDIT: Search Code</p>
  
  <pre><code>private void btnSearch_Click(object sender, EventArgs e)
          {
              lstResult.Items.Clear();
  
              if (txtSearch.Text == String.Empty)
              {
                  MessageBox.Show("The textbox is empty, there is nothing to search.",
                      "Textbox empty", MessageBoxButtons.OK, MessageBoxIcon.Information);
              }
              else
              {
                  this.CheckFormatting();
              }
  
          }
  
          private long GetIndexForCoord(string coord)
          {
              // gets out a numerical value for each coordinate to make it easier to compare
              Regex m_regex = new Regex("\\d\\d:\\d\\d:\\d\\d:\\d\\d");
              string cleaned = m_regex.Match(coord).Value;
              cleaned = cleaned.Replace(':', '0');
              return Convert.ToInt64(cleaned);
          }
  
          private List&lt;string&gt; GetResults(string coord)
          {
              // gets out the 5 closest coordinates
              long index = GetIndexForCoord(coord);
  
              // First find the 5 closest indexes to the one we're looking for
              List&lt;long&gt; found = new List&lt;long&gt;();
              while (found.Count &lt; 5)
              {
                  long closest = long.MaxValue;
                  long closestAbs = long.MaxValue;
                  foreach (long i in m_indexes)
                  {
                      if (!found.Contains(i))
                      {
                          long absIndex = Math.Abs(index - i);
                          if (absIndex &lt; closestAbs)
                          {
                              closest = i;
                              closestAbs = absIndex;
                          }
                      }
                  }
                  if (closest != long.MaxValue)
                  {
                      found.Add(closest);
                  }
              }
  
              // Then use those indexes to get the coordinates from the dictionary
              List&lt;string&gt; s = new List&lt;string&gt;();
              foreach (long i in found)
              {
                  s.Add(m_dic[i]);
              }
              return s;
          }
  
          private void CheckFormatting()
          {
              StringReader objReader = new StringReader(txtSearch.Text);
  
              bool FlagCheck = true;
  
                  if (!Regex.IsMatch(txtSearch.Text,
                      "G3[0-9]{1}:[0-9]{2}:[0-9]{2}:[0-9]{2}"))
                  {
                      FlagCheck = false;
                  }
  
              if (FlagCheck == true)
              {
                  this.CheckAndPopulate();
              }
              else
              {
                  MessageBox.Show("Your search coordinates are not formatted correctly.",
                         "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
              }
          }
  
          private void CheckAndPopulate()
          {
              StreamReader objReader = new StreamReader("Jumpgate List.JG");
              List&lt;String&gt; v = new List&lt;String&gt;();
              do
              {
                  v.Add(objReader.ReadLine());
              }
              while (objReader.Peek() != -1);
  
              objReader.Close();
  
              foreach (string c in v)
              {
                  long index = GetIndexForCoord(c);
                  m_dic.Add(index, c);
                  m_indexes.Add(index);
              }
  
              List&lt;string&gt; results = GetResults(txtSearch.Text);
              foreach (string c in results)
              {
                  lstResult.Items.Add(c);
              }
          }
  </code></pre>