Triggering events with Windows Presentation Form Controls

JohnCTX 636 Reputation points
2021-08-18T00:34:01.113+00:00

I am having too many issues on how either to raise events or how to create delegates?

For example:

    private void rtb_TextChanged_6(object sender, TextChangedEventArgs e)
        {
            if (sender.Equals("Hello"))
            {
                MessageBox.Show("Will you count me in?");
            }
            //program does not display message box instead.

        }

Note that rtb is the name of a WPF Rich Text Box control.
It runs okay, but when I typed, Hello, message box does not appear.

What am I missing?

Developer technologies | Windows Presentation Foundation
Developer technologies | XAML
Developer technologies | XAML
A language based on Extensible Markup Language (XML) that enables developers to specify a hierarchy of objects with a set of properties and logic.
Developer technologies | C#
Developer technologies | C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
0 comments No comments
{count} votes

Answer accepted by question author
  1. Hui Liu-MSFT 48,711 Reputation points Microsoft External Staff
    2021-08-18T03:13:13.74+00:00

    You could try to refer to the following code to extract text from RichTextBox and match the string.
    The code of xaml:

    <StackPanel>  
    <RichTextBox  Name="rtbPaste" Height="50" TextChanged="rtbPaste_TextChanged" ></RichTextBox>  
    </StackPanel>  
    

    The code of xaml.cs:

    private void rtbPaste_TextChanged(object sender, TextChangedEventArgs e)  
        {  
         TextRange textRange = new TextRange(rtbPaste.Document.ContentStart, rtbPaste.Document.ContentEnd);  
          string _Text = textRange.Text.Replace(Environment.NewLine, "");  
          if (_Text.Equals("Hello"))  
          {  
            MessageBox.Show("Will you count me in?");  
          }  
    }  
    

    The picture of result:
    124171-2.gif

    1 person found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Sam of Simple Samples 5,571 Reputation points
    2021-08-18T01:08:19.483+00:00

    Did you use the properties of the RichTextBox to add a handler? If there is not a handler there as shown in the following image then just double-click on the empty box for the handler and VS will create it for you.

    124111-t.jpg


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.