/// <summary>
        /// When the text in the editTextBox changes, fire off the query to populate the statement completion popup.
        /// </summary>
        private void DoStatementCompletion()
        {
            if (DisableCompletion)
                return;

            String searchText = TextBox.Text;
            if (searchText.Length == 0)
            {
                HideStatementCompletionForm();
                return;
            }
            if (_proteinMatcherLast != null)
            {
                //if (_proteinMatcherLast.Settings.SearchText == searchText)
                //{
                //    // Query is already executing for the text that the user typed: no work to do now.
                //    return;
                //}
                if (searchText.StartsWith(_proteinMatcherLast.Settings.SearchText))
                {
                    // If the new text is just longer than the previous text, see if we can just refine the results
                    // of the previous query.
                    var results = _proteinMatcherLast.GetMatches();
                    var newResults = new List<ProteinMatch>();
                    if (results != null)
                    {
                        var oldSettings = _proteinMatcherLast.Settings;
                        var newSettings = new ProteinMatchSettings(oldSettings.ProteomeDbPath, oldSettings.Protease, MatchTypes, searchText);
                        newResults.AddRange(RefineMatches(results, newSettings));
                    }
                    if (newResults.Count == 0)
                    {
                        // Nothing from the previous query matches: hide the statement completion form.
                        HideStatementCompletionForm();
                        _proteinMatcherLast = null;
                    }
                    else
                    {
                        // Display the refined results: we'll still fire off another query to see if we get better results.
                        DisplayResultsNow(newResults, _proteinMatcherLast.MaxResults);
                    }
                }
            }

            if (_proteinMatcher != null)
            {
                _proteinMatcher.Cancel();
                _proteinMatcher = null;
            }
            var settings = CreateProteinMatchSettings(_documentUiContainer.DocumentUI, MatchTypes, searchText);
            if (settings != null)
            {
                _proteinMatcher = new ProteinMatchQuery(settings);
                _proteinMatcher.BeginExecute(DisplayResults);
            }
        }
 /// <summary>
 /// Called on a background thread when the statement completion query has some results to display.
 /// </summary>
 private void DisplayResults(ProteinMatchQuery proteinMatchQuery)
 {
     if (TextBox == null || TextBox.IsDisposed)
     {
         return;
     }
     TextBox.BeginInvoke(new Action<ProteinMatchQuery>(DisplayResultsNow), proteinMatchQuery);
 }
 /// <summary>
 /// Updates the StatementCompletion popup to show the list of matching results.
 /// </summary>
 private void DisplayResultsNow(ProteinMatchQuery proteinMatchQuery)
 {
     if (proteinMatchQuery != _proteinMatcher)
     {
         return;
     }
     DisplayResultsNow(proteinMatchQuery.GetMatches(), proteinMatchQuery.MaxResults);
     _proteinMatcherLast = proteinMatchQuery;
 }
 public void Detach()
 {
     if (TextBox == null)
     {
         return;
     }
     HideStatementCompletionForm();
     if (_proteinMatcher != null)
     {
         _proteinMatcher.Cancel();
         _proteinMatcher = null;
     }
     if (null != _proteomeDb)
     {
         _proteomeDb.Dispose();
         _proteomeDb = null;
     }
     TextBox.KeyDown -= TextBox_KeyDown;
     TextBox.TextChanged -= TextBox_TextChanged;
     TextBox.GotFocus -= TextBox_GotFocus;
     TextBox.LostFocus -= TextBox_LostFocus;
     TextBox.LocationChanged -= TextBox_LocationChanged;
     TextBox = null;
 }