2

I have a WPF with a RichTextBox. Text inside de RichTextBox doesn't fit the box and is not displayed. I would like text to return to a new line when the end of line is reached.

There are many question related like this question on StackOverflow or this blog but I am surprised I can't apply them: Text Wrapping seems to be native - but it doesn't for me. And there are functions that doesn't exist like myRTBox.WordWrap is not available ?

Are there different types of RichTextBox (I use System.Windows.Controls.RichTextBox)? If not do you have an idea to force wrapping ?

public partial class UserControl1 : System.Windows.Controls.UserControl
{
   public UserControl1()
   {
       InitializeComponent();
       RTBox.Document.Blocks.Clear();
       RTBox.AppendText("a long message to test how it will be displayed in a rich text box");
...

and the xaml

<RichTextBox x:Name="RTBox" IsReadOnly="True " HorizontalAlignment="Left" Height="65" Margin="12,16,0,0" VerticalAlignment="Top" Width="168" Background="#AAEA900F" HorizontalScrollBarVisibility="Disabled">
        <FlowDocument Language="en-us">
            <Paragraph>
                <Run Text="Test to display a text larger than a single line"/>
            </Paragraph>
        </FlowDocument>
    </RichTextBox>
Freddlow
  • 167
  • 1
  • 12
  • Do you use `Wpf` or `WinForms`? – FoggyFinder May 27 '18 at 09:53
  • Sorry I confused. I use WPF (and I have just edited the question to correct it) – Freddlow May 27 '18 at 10:02
  • great, can you show the xaml? – FoggyFinder May 27 '18 at 10:06
  • As Steve answered, I used System.Windows.Controls.RichTextBox and not System.Windows.Forms.RichTextBox. But I can't add System.Windows.Forms.RichTextBox to WPF with the interface I have to add it manually (I don't know how to add it yet, it dosen't display). Which part xaml would you like to see ? I added the previous one with System.Windows.Controls.RichTextBox in edit inquestion – Freddlow May 27 '18 at 10:39
  • well, I'd want to see [MCVE](https://stackoverflow.com/help/mcve) – FoggyFinder May 27 '18 at 11:03
  • I think you can avoid using `WinForms` controls and use `System.Windows.Controls.RichTextBox` – FoggyFinder May 27 '18 at 11:04
  • @Foggy Finder : I 've just added xaml to the question – Freddlow May 27 '18 at 11:37
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/171863/discussion-between-foggy-finder-and-freddlow). – FoggyFinder May 27 '18 at 12:07

2 Answers2

2

You are using the RichTextBox for WPF not the standard one for WinForms.
The correct namespace is System.Windows.Forms.RichTextBox

This example using LinqPad show you the WordWrap is available

void Main()
{
    Form f = new Form();
    System.Windows.Forms.RichTextBox RTBox = new System.Windows.Forms.RichTextBox();
    RTBox.Clear();
    RTBox.WordWrap = true;
    RTBox.AppendText("a long message to test how it will be displayed in a rich text box");
    f.Controls.Add(RTBox);
    f.Show();
}
Steve
  • 213,761
  • 22
  • 232
  • 286
  • thank you ! Do you know how to add a System.Windows.Forms.RichTextBox in a WPF ? I put `RTBox = new System.Windows.Forms.RichTextBox(); RTBox.Left = 8; RTBox.Top = 6; RTBox.Text = "test text"; RTBox.Visible = true;` in public UserControl1() of my WPF but it doesn't display – Freddlow May 27 '18 at 09:10
  • Sorry but I am not an expert for WPF. I have answered your question because you have tagged it with WinForms. If you want an answer for WPF you really need to use the correct tag to attract people versed on that technology. I suggest you to post a new question detailing the problem that you currently have – Steve May 27 '18 at 09:37
1

Try this:

RTBox.Multiline = true;
Sketch Turner
  • 55
  • 1
  • 6