question

JohnCTX-6479 avatar image
0 Votes"
JohnCTX-6479 asked JohnCTX-6479 commented

Triggering events with Windows Presentation Form Controls

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?

dotnet-csharpwindows-wpfdotnet-wpf-xaml
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

HuiLiu-MSFT avatar image
1 Vote"
HuiLiu-MSFT answered HuiLiu-MSFT edited

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





2.gif (68.3 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

SimpleSamples avatar image
0 Votes"
SimpleSamples answered JohnCTX-6479 commented

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



t.jpg (43.1 KiB)
· 8
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Yes, I did use the properties tab.
I am going to download Microsoft Blend, and see if that mechanism works.

0 Votes 0 ·

I have quickly migrated to Microsoft Blend.

Maybe this would give me better control over triggering events.

0 Votes 0 ·

I have tried and run the code in the following events under the assigned WPF Rich Text Box control.
TextInput and TextChanged

Still no progress.

0 Votes 0 ·
Show more comments