Bend.MainWindow.FindNextStringOnPage C# (CSharp) Method

FindNextStringOnPage() private method

private FindNextStringOnPage ( string findText, bool backward, bool resetSearch, bool matchCase, bool useRegex ) : void
findText string
backward bool
resetSearch bool
matchCase bool
useRegex bool
return void
        internal void FindNextStringOnPage(string findText, bool backward, bool resetSearch, bool matchCase, bool useRegex)
        {
            if (this.currentTabIndex >= 0 && this.currentTabIndex < this.tab.Count)
            {
                bool thisSearchWasFromBegin;
                int matchLength = 0;
            repeatSearch:
                if (backward)
                {
                    // Search backwards
                    currentSearchIndex--;
                    if (resetSearch || currentSearchIndex < 0 || currentSearchIndex >= tab[this.currentTabIndex].TextEditor.Text.Length) currentSearchIndex = 0;
                    thisSearchWasFromBegin = (currentSearchIndex == 0);
                    currentSearchIndex = tab[this.currentTabIndex].TextEditor.Text.LastIndexOf(findText, this.currentSearchIndex, matchCase ? StringComparison.Ordinal : StringComparison.OrdinalIgnoreCase);
                    matchLength = findText.Length;
                }
                else
                {
                    // Search forwards
                    currentSearchIndex++;
                    if (resetSearch || currentSearchIndex < 0 || currentSearchIndex >= tab[this.currentTabIndex].TextEditor.Text.Length) currentSearchIndex = 0;
                    thisSearchWasFromBegin = (currentSearchIndex == 0);
                    if (useRegex)
                    {
                        try
                        {
                            System.Text.RegularExpressions.Regex regEx = new System.Text.RegularExpressions.Regex(findText, matchCase ? System.Text.RegularExpressions.RegexOptions.None : System.Text.RegularExpressions.RegexOptions.IgnoreCase);
                            System.Text.RegularExpressions.Match regExMatch = regEx.Match(tab[this.currentTabIndex].TextEditor.Text, currentSearchIndex);
                            if (regExMatch.Success)
                            {
                                currentSearchIndex = regExMatch.Index;
                                matchLength = regExMatch.Length;
                            }
                            else
                            {
                                currentSearchIndex = -1;
                            }
                        }
                        catch
                        {
                            currentSearchIndex = -1;
                        }
                    }
                    else
                    {
                        currentSearchIndex = tab[this.currentTabIndex].TextEditor.Text.IndexOf(findText, this.currentSearchIndex, matchCase ? StringComparison.Ordinal : StringComparison.OrdinalIgnoreCase);
                        matchLength = findText.Length;
                    }
                }
                if (!this.lastKeyWasEnter)
                {
                    this.lastKeyWasEnter = true;
                    goto repeatSearch;
                }

                if (currentSearchIndex >= 0)
                {
                    tab[this.currentTabIndex].TextEditor.Select(currentSearchIndex, matchLength, /*isFindOnPageSelection*/true);

                    // We are looping and changed to status text to "NO MORE MATCHES", change it back to the match count.
                    if (thisSearchWasFromBegin)
                    {
                        UpdateFind(findText, false, matchCase, useRegex);
                    }
                }
                else
                {
                    if (thisSearchWasFromBegin)
                    {
                        this.SetStatusText("NO MATCHES FOUND");
                    }
                    else
                    {
                        this.SetStatusText("NO MORE MATCHES");
                    }
                    tab[this.currentTabIndex].TextEditor.Select(0, 0);
                }
            }
        }

Usage Example

Ejemplo n.º 1
0
 private void Find_KeyUp(object sender, KeyEventArgs e)
 {
     if (e.Key == Key.Enter)
     {
         ComboBox comboBox = ((ComboBox)sender);
         if (comboBox.Text.Length > 0 && comboBox.Items != null && (comboBox.Items.Count == 0 || (string)comboBox.Items[0] != comboBox.Text))
         {
             comboBox.Items.Insert(0, comboBox.Text);
         }
         TraversalRequest tRequest      = new TraversalRequest(FocusNavigationDirection.Next);
         UIElement        keyboardFocus = Keyboard.FocusedElement as UIElement;
         if (keyboardFocus != null)
         {
             keyboardFocus.MoveFocus(tRequest);
         }
     }
     else
     {
         mainWindow.FindNextStringOnPage(FindText.Text, false, true, this.MatchCase.IsChecked ?? true, this.RegexFind.IsChecked ?? true);
     }
 }