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:
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:
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.
3: https://learn.microsoft.com/en-us/answers/articles/67444/email-notifications.html