diff --git a/dotnet-desktop-guide/samples/snippets/csharp/VS_Snippets_Wpf/DialogBoxSample/CSharp/FindDialogBox.xaml.cs b/dotnet-desktop-guide/samples/snippets/csharp/VS_Snippets_Wpf/DialogBoxSample/CSharp/FindDialogBox.xaml.cs index b900a19f1d..06375ce6a5 100644 --- a/dotnet-desktop-guide/samples/snippets/csharp/VS_Snippets_Wpf/DialogBoxSample/CSharp/FindDialogBox.xaml.cs +++ b/dotnet-desktop-guide/samples/snippets/csharp/VS_Snippets_Wpf/DialogBoxSample/CSharp/FindDialogBox.xaml.cs @@ -50,19 +50,31 @@ public int Length void findNextButton_Click(object sender, RoutedEventArgs e) { + string textToFind = this.findWhatTextBox.Text; + + // Short-circuit: Did the user not provide any text at all? + if (string.IsNullOrEmpty(textToFind)) return; + // Find matches if (this.matches == null) { - string pattern = this.findWhatTextBox.Text; + RegexOptions regexOptions = RegexOptions.None; + + // Escape the user-typed text in case it contains regex special characters. + string pattern = Regex.Escape(textToFind); // Match whole word? - if ((bool)this.matchWholeWordCheckBox.IsChecked) pattern = @"(?<=\W{0,1})" + pattern + @"(?=\W)"; + if ((bool)this.matchWholeWordCheckBox.IsChecked) + { + if (BeginsWithRegexWordChar(textToFind)) pattern = @"\b" + pattern; + if (EndsWithRegexWordChar(textToFind)) pattern = pattern + @"\b"; + } // Case sensitive - if (!(bool)this.caseSensitiveCheckBox.IsChecked) pattern = "(?i)" + pattern; + if (!(bool)this.caseSensitiveCheckBox.IsChecked) regexOptions |= RegexOptions.IgnoreCase; // Find matches - this.matches = Regex.Matches(this.textBoxToSearch.Text, pattern); + this.matches = Regex.Matches(this.textBoxToSearch.Text, pattern, regexOptions); this.matchIndex = 0; // Word not found? @@ -77,7 +89,7 @@ void findNextButton_Click(object sender, RoutedEventArgs e) // Start at beginning of matches if the last find selected the last match if (this.matchIndex == this.matches.Count) { - MessageBoxResult result = MessageBox.Show("Nmore matches found. Start at beginning?", "Find", MessageBoxButton.YesNo); + MessageBoxResult result = MessageBox.Show("No more matches found. Start at beginning?", "Find", MessageBoxButton.YesNo); if (result == MessageBoxResult.No) return; // Reset @@ -96,6 +108,18 @@ void findNextButton_Click(object sender, RoutedEventArgs e) this.matchIndex++; } + static bool BeginsWithRegexWordChar(string literal) + { + if (string.IsNullOrEmpty(literal)) return false; + return Regex.IsMatch(literal[0].ToString(), @"\w"); + } + + static bool EndsWithRegexWordChar(string literal) + { + if (string.IsNullOrEmpty(literal)) return false; + return Regex.IsMatch(literal[literal.Length - 1].ToString(), @"\w"); + } + void textBoxToSearch_TextChanged(object sender, TextChangedEventArgs e) { ResetFind(); diff --git a/dotnet-desktop-guide/samples/snippets/visualbasic/VS_Snippets_Wpf/DialogBoxSample/VisualBasic/FindDialogBox.xaml.vb b/dotnet-desktop-guide/samples/snippets/visualbasic/VS_Snippets_Wpf/DialogBoxSample/VisualBasic/FindDialogBox.xaml.vb index ec81e59a32..aed591c9e0 100644 --- a/dotnet-desktop-guide/samples/snippets/visualbasic/VS_Snippets_Wpf/DialogBoxSample/VisualBasic/FindDialogBox.xaml.vb +++ b/dotnet-desktop-guide/samples/snippets/visualbasic/VS_Snippets_Wpf/DialogBoxSample/VisualBasic/FindDialogBox.xaml.vb @@ -33,15 +33,28 @@ Namespace SDKSample End Sub Private Sub findNextButton_Click(ByVal sender As Object, ByVal e As RoutedEventArgs) + Dim textToFind As String = Me.findWhatTextBox.Text + + ' Short-circuit: Did the user not provide any text at all? + If String.IsNullOrEmpty(textToFind) Then Return + If (Me.matches Is Nothing) Then - Dim pattern As String = Me.findWhatTextBox.Text + Dim matchOptions As RegexOptions = RegexOptions.None + + ' Escape the user-typed text in case it contains regex special characters. + Dim pattern As String = Regex.Escape(textToFind) + + ' Match whole word? If Me.matchWholeWordCheckBox.IsChecked.Value Then - pattern = ("(?<=\W{0,1})" & pattern & "(?=\W)") - End If - If Not Me.caseSensitiveCheckBox.IsChecked.Value Then - pattern = ("(?i)" & pattern) + If BeginsWithRegexWordChar(textToFind) Then pattern = "\b" & pattern + If EndsWithRegexWordChar(textToFind) Then pattern = pattern & "\b" End If - Me.matches = Regex.Matches(Me.textBoxToSearch.Text, pattern) + + ' Case sensitive + If Not Me.caseSensitiveCheckBox.IsChecked.Value Then matchOptions = matchOptions Or RegexOptions.IgnoreCase + + ' Find matches + Me.matches = Regex.Matches(Me.textBoxToSearch.Text, pattern, matchOptions) Me.matchIndex = 0 If (Me.matches.Count = 0) Then MessageBox.Show(("'" & Me.findWhatTextBox.Text & "' not found."), "Find") @@ -50,7 +63,7 @@ Namespace SDKSample End If End If If (Me.matchIndex = Me.matches.Count) Then - Dim result As MessageBoxResult = MessageBox.Show("Nmore matches found. Start at beginning?", "Find", MessageBoxButton.YesNo) + Dim result As MessageBoxResult = MessageBox.Show("No more matches found. Start at beginning?", "Find", MessageBoxButton.YesNo) If (result = MessageBoxResult.No) Then Return End If @@ -65,6 +78,16 @@ Namespace SDKSample Me.matchIndex += 1 End Sub + Private Shared Function BeginsWithRegexWordChar(ByVal literal As String) As Boolean + If String.IsNullOrEmpty(literal) Then Return False + Return Regex.IsMatch(literal(0).ToString(), "\w") + End Function + + Private Shared Function EndsWithRegexWordChar(ByVal literal As String) As Boolean + If String.IsNullOrEmpty(literal) Then Return False + Return Regex.IsMatch(literal(literal.Length - 1).ToString(), "\w") + End Function + Private Sub findWhatTextBox_TextChanged(ByVal sender As Object, ByVal e As TextChangedEventArgs) Me.ResetFind() End Sub