How to trigger a Text Changed event of a WPF Rich Text Box?

JohnCTX 636 Reputation points
2021-08-16T04:11:48.137+00:00

I have this slight issue when triggering events for Windows Presentation Foundation forms.

I want to key in words that trigger a Text Changed event in a Rich Text Box control within the Main Windows WPF Form.

Should I need to alter its coding in the XAML source?

Are there any steps to perform that task?

Developer technologies Windows Presentation Foundation
Developer technologies C#
{count} votes

1 answer

Sort by: Most helpful
  1. Hui Liu-MSFT 48,676 Reputation points Microsoft External Staff
    2021-08-16T07:59:05.783+00:00

    RichTextBox class inherits the TextBoxBase class with TextChanged event. So you could try to use TextBoxBase.TextChanged event in RichTextBox. And the TextChanged event is automatically triggered when you enter text.
    The code of xaml:

    <StackPanel Name="sp">  
            <RichTextBox Name="rtb" Width="200" Height="40"   TextChanged="rtb_TextChanged"/>  
    </StackPanel>  
    

    The code of xaml.cs:

    private void rtb_TextChanged(object sender, EventArgs e)  
    {  
       rtb.Background = Brushes.AliceBlue;  
    }  
    

    The picture of result:
    123506-1.gif

    Update:
    You could find WPF controls here . And MainWindow is not a control. For passing values to RichTextBox in the same MainWindow, you could try to refer to the following code.
    The code of xaml:

    <StackPanel>  
            <TextBox x:Name="tb" Text="hello,how are you?" Height="50" Background="LightBlue"/>  
            <RichTextBox  Name="rtbPaste" Height="50" TextChanged="rtbPaste_TextChanged" ></RichTextBox>  
            <StackPanel Orientation="Horizontal">  
                <Button Name="copyBtn" Width="100" Height="40" Content="copy" Click="copyBtn_Click"></Button>  
                <Button Name="pasteBtn" Width="100" Height="40" Content="paste" Click="pasteBtn_Click"></Button>  
                <Button Name="pasteBtn1" Width="200" Height="40" Content="get text from TextBox to RichTextBox " Click="pasteBtn1_Click"></Button>  
            </StackPanel>  
    </StackPanel>  
    

    The code of xaml.cs:

    using System.IO;  
    using System.Text;  
    using System.Windows;  
    using System.Windows.Controls;  
    using System.Windows.Documents;  
    using System.Windows.Media;  
    
    namespace FontRenderColor  
    {  
      public partial class MainWindow : Window  
      {  
        public MainWindow()  
        {  
          InitializeComponent();  
        }  
        private void copyBtn_Click(object sender, RoutedEventArgs e)  
        {  
          if (!string.IsNullOrEmpty(tb.Text))  
            Clipboard.SetText(tb.Text);  
        }  
        private void pasteBtn_Click(object sender, RoutedEventArgs e)  
        {  
          if (Clipboard.ContainsText(TextDataFormat.Text))  
            rtbPaste.AppendText(Clipboard.GetText(TextDataFormat.Text).ToString());  
        }  
        private void pasteBtn1_Click(object sender, RoutedEventArgs e)  
        {  
          string txtString = tb.Text;  
          FlowDocument fd = new FlowDocument();  
          MemoryStream ms = new MemoryStream(Encoding.ASCII.GetBytes(txtString));  
          TextRange textRange = new TextRange(fd.ContentStart, fd.ContentEnd);  
          textRange.Load(ms, DataFormats.Rtf);  
          rtbPaste.Document = fd;  
        }  
        private void rtbPaste_TextChanged(object sender, TextChangedEventArgs e)  
        {  
          rtbPaste.Background = Brushes.Pink;  
        }  
      }  
    }  
    

    The picture of picture:

    124123-1.gif


    If the response is helpful, please click "Accept Answer" and upvote it. 
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread. 

    3https://learn.microsoft.com/en-us/answers/articles/67444/email-notifications.html

    1 person found this answer helpful.
    0 comments No comments

Your answer

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