question

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

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

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?

dotnet-csharpwindows-wpf
· 7
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.


Did you do this task in Microsoft Blend?

Because I have tried to copy and paste data from a non-WPF control to a WPF control.

I have tried everything in this task.

Am I missing something?

Regards,

JohnCTX

0 Votes 0 ·

Hi,@ JohnCTX-6479 . What does this sentence Because I have tried to copy and paste data from a non-WPF control to a WPF control. mean? Could you show me more information about what you really want to do? And what does non-wpf control specifically refer to? Is it a control in Microsoft Blend or something else?

0 Votes 0 ·

I shall rephrase the question:

How should I pass parameters from a text box control to a WPF rich text box control?

0 Votes 0 ·
Show more comments

1 Answer

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

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. 


1.gif (25.8 KiB)
1.gif (54.7 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.