/// <summary>
        /// Attempts to parse a string as a Postcode.
        /// </summary>
        /// <param name="s">String to be parsed</param>
        /// <param name="result">Postcode object</param>
        /// <param name="incodeMandatory">
        /// Indicates that the string passed must include a valid inner code.
        /// </param>
        /// <returns>
        /// Boolean indicating whether the string was successfully parsed as a UK Postcode
        /// </returns>
        public static bool TryParse(string s, out Postcode result, bool incodeMandatory)
        {
            // Set output to new Postcode
            result = new Postcode();

            // Copy the input before messing with it
            string input = s;

            // Guard clause - check for null or empty string
            if (string.IsNullOrEmpty(input))
            {
                return(false);
            }

            // uppercase input and strip undesirable characters
            input = Regex.Replace(input.ToUpperInvariant(), "[^A-Z0-9]", string.Empty);

            // guard clause - input is more than seven characters
            if (input.Length > 7)
            {
                return(false);
            }

            #region BS7666 Matching

            // Try to match full standard postcode
            Match fullMatch = Regex.Match(input, regexBS7666Full);
            if (fullMatch.Success)
            {
                result.OutCode = fullMatch.Groups["outCode"].Value;
                result.InCode  = fullMatch.Groups["inCode"].Value;
                return(true);
            }

            // Try to match outer standard postcode only
            Match outerMatch = Regex.Match(input, regexBS7666OuterStandAlone);
            if (outerMatch.Success)
            {
                if (incodeMandatory)
                {
                    return(false);
                }

                result.OutCode = outerMatch.Groups["outCode"].Value;
                return(true);
            }

            #endregion

            #region BFPO Matching

            // Try to match full BFPO postcode
            Match bfpoFullMatch = Regex.Match(input, regexBfpoFull);
            if (bfpoFullMatch.Success)
            {
                result.OutCode = bfpoFullMatch.Groups["outCode"].Value;
                result.InCode  = bfpoFullMatch.Groups["inCode"].Value;
                return(true);
            }

            // Try to match outer BFPO postcode
            Match bfpoOuterMatch = Regex.Match(input, regexBfpoOuterStandalone);
            if (bfpoOuterMatch.Success)
            {
                if (incodeMandatory)
                {
                    return(false);
                }

                result.OutCode = bfpoOuterMatch.Groups["outCode"].Value;
                return(true);
            }

            #endregion

            #region Exceptions to the rule matching

            // Loop through exceptions to the rule
            for (int i = 0; i < exceptionsToTheRule.GetLength(0); i++)
            {
                // Check for a full match
                if (input == string.Concat(exceptionsToTheRule[i, 0], exceptionsToTheRule[i, 1]))
                {
                    result.OutCode = exceptionsToTheRule[i, 0];
                    result.InCode  = exceptionsToTheRule[i, 1];
                    return(true);
                }

                // Check for partial match only
                if (input == exceptionsToTheRule[i, 0])
                {
                    if (incodeMandatory)
                    {
                        return(false);
                    }

                    result.OutCode = exceptionsToTheRule[i, 0];
                    return(true);
                }
            }

            #endregion

            return(false);
        }
 /// <summary>
 /// Attempts to parse a string as a Postcode.
 /// </summary>
 /// <param name="s">String to be parsed</param>
 /// <param name="result">Postcode object</param>
 /// <returns>
 /// Boolean indicating whether the string was successfully parsed as a UK Postcode
 /// </returns>
 /// <remarks>Using this overload, the inner code is not mandatory.</remarks>
 public static bool TryParse(string s, out Postcode result)
 {
     return(TryParse(s, out result, false));
 }
       /// <summary>  
       /// Parses a string as a Postcode.  
       /// </summary>  
       /// <param name="s">String to be parsed</param>  
       /// <param name="incodeMandatory">  
       /// Indicates that the string passed must include a valid inner code.  
       /// </param>  
       /// <returns>Postcode object</returns>  
       /// <exception cref="FormatException">  
       /// If the passed string cannot be parsed as a UK postcode  
       /// </exception>  
       public static Postcode Parse(string s, bool incodeMandatory)  
       {  
           Postcode p = new Postcode();  
           if (TryParse(s, out p, incodeMandatory))  
               return p;  
 
           throw new FormatException();  
       }  
        /// <summary>  
        /// Attempts to parse a string as a Postcode.  
        /// </summary>  
        /// <param name="s">String to be parsed</param>  
        /// <param name="result">Postcode object</param>  
        /// <param name="incodeMandatory">  
        /// Indicates that the string passed must include a valid inner code.  
        /// </param>  
        /// <returns>  
        /// Boolean indicating whether the string was successfully parsed as a UK Postcode  
        /// </returns>  
        public static bool TryParse(string s, out Postcode result, bool incodeMandatory)  
        {  
            // Set output to new Postcode  
            result = new Postcode();  
  
            // Copy the input before messing with it  
            string input = s;  
  
            // Guard clause - check for null or empty string  
            if (string.IsNullOrEmpty(input)) return false;  
  
            // uppercase input and strip undesirable characters  
            input = Regex.Replace(input.ToUpperInvariant(), "[^A-Z0-9]", string.Empty);  
  
            // guard clause - input is more than seven characters  
            if (input.Length > 7) return false;  
 
            #region BS7666 Matching  
  
            // Try to match full standard postcode  
            Match fullMatch = Regex.Match(input, regexBS7666Full);  
            if (fullMatch.Success)  
            {  
                result.OutCode = fullMatch.Groups["outCode"].Value;  
                result.InCode = fullMatch.Groups["inCode"].Value;  
                return true;  
            }  
  
            // Try to match outer standard postcode only  
            Match outerMatch = Regex.Match(input, regexBS7666OuterStandAlone);  
            if (outerMatch.Success)  
            {  
                if (incodeMandatory) return false;  
  
                result.OutCode = outerMatch.Groups["outCode"].Value;  
                return true;  
            }  
 
            #endregion  
 
            #region BFPO Matching  
  
            // Try to match full BFPO postcode  
            Match bfpoFullMatch = Regex.Match(input, regexBfpoFull);  
            if (bfpoFullMatch.Success)  
            {  
                result.OutCode = bfpoFullMatch.Groups["outCode"].Value;  
                result.InCode = bfpoFullMatch.Groups["inCode"].Value;  
                return true;  
            }  
  
            // Try to match outer BFPO postcode  
            Match bfpoOuterMatch = Regex.Match(input, regexBfpoOuterStandalone);  
            if (bfpoOuterMatch.Success)  
            {  
                if (incodeMandatory) return false;  
  
                result.OutCode = bfpoOuterMatch.Groups["outCode"].Value;  
                return true;  
            }  
 
            #endregion  
 
            #region Exceptions to the rule matching  
  
            // Loop through exceptions to the rule  
            for (int i = 0; i < exceptionsToTheRule.GetLength(0); i++)  
            {  
                // Check for a full match  
                if (input == string.Concat(exceptionsToTheRule[i, 0], exceptionsToTheRule[i, 1]))  
                {  
                    result.OutCode = exceptionsToTheRule[i,0];  
                    result.InCode = exceptionsToTheRule[i,1];  
                    return true;  
                }  
  
                // Check for partial match only  
                if (input == exceptionsToTheRule[i,0])  
                {  
                    if (incodeMandatory) return false;  
  
                    result.OutCode = exceptionsToTheRule[i,0];  
                    return true;  
                }  
            }  
 
            #endregion  
  
            return false;  
        }  
 /// <summary>  
 /// Attempts to parse a string as a Postcode.  
 /// </summary>  
 /// <param name="s">String to be parsed</param>  
 /// <param name="result">Postcode object</param>  
 /// <returns>  
 /// Boolean indicating whether the string was successfully parsed as a UK Postcode  
 /// </returns>  
 /// <remarks>Using this overload, the inner code is not mandatory.</remarks>  
 public static bool TryParse(string s, out Postcode result)  
 {  
     return TryParse(s, out result, false);  
 }  
        void backgroundWorkerSearcher_DoWork(object sender, DoWorkEventArgs e)
        {
            OnSetStatusLabelValue("Searching...");
            this.listViewPatients.ListViewItemSorter = null;
            AddFoundPatientCallback d = new AddFoundPatientCallback(AddFoundPatient);
            List<ListViewItem> _temp = new List<ListViewItem>();


            //build up the search terms
            string split = " ";
            string[] initialSsearchTerms = textBoxSearch.Text.Split(split.ToCharArray());
            List<string> tempSearchTerms = new List<string>();
            string tempSearchTerm = "";
            Postcode postcode = new Postcode();
            //check all but the last for a post code
            for (int i = 0; i < initialSsearchTerms.Length -1; i++)
            {
                //If its a valid
                if (Postcode.TryParse(initialSsearchTerms[i] + initialSsearchTerms[i + 1], out postcode, true))
                {
                    tempSearchTerm = initialSsearchTerms[i] + " "+ initialSsearchTerms[i+1];
                    i++;
                }
                else
                    tempSearchTerm = initialSsearchTerms[i];
                tempSearchTerms.Add(tempSearchTerm);
            }
            //if a post code has not been found the add the last term
            if (postcode.InCode == null)
            {
                tempSearchTerm = initialSsearchTerms[initialSsearchTerms.Length - 1];
                tempSearchTerms.Add(tempSearchTerm);
            }
            else if (!postcode.InCode.StartsWith(initialSsearchTerms[initialSsearchTerms.Length - 1])) // if one has been found but doesn't match then add last term
            {
                tempSearchTerm = initialSsearchTerms[initialSsearchTerms.Length - 1];
                tempSearchTerms.Add(tempSearchTerm);
            }
            string[] searchTerms = tempSearchTerms.ToArray();


            //search for the terms, each term has to be found in an item
            bool matchedTerm = false;
            for (int i = 0; i < _listViewItems.Count; i++)
            {
                foreach (string term in searchTerms)
                {
                    matchedTerm = false;
                    for (int j = 0; j < _listViewItems[i].SubItems.Count; j++)
                    {
                        if (_listViewItems[i].SubItems[j].Text.StartsWith(term))
                        {
                            matchedTerm = true;
                        }
                    }
                    if (!matchedTerm)
                        break;
                }
                if (matchedTerm)
                {
                    this.Invoke(d, new object[] { _listViewItems[i] });
                    OnSetProgressBarValue(i * 100 / _listViewItems.Count);
                }
                
            }
            
        }