8

So, I have a RichTextBox (shown below) which has a monospaced font, and must have the same number of characters on each line. Doing this requires inserting a \n newline character after every x characters in the string that feeds the box.

I also have an algorithm that allows the cursor to move around with the press of the arrow keys, selecting individual characters as it moves. When the cursor covers a word in the box though, it should highlight the whole word.

Problem: When a word spans mutliple lines, there is an empty space following the final character of the first line, which I assume is caused by the \n character.

How can I make the RichTextBox.Selection.Select() not highlight these blank spaces? I can strip the selection of the character when parsing and such, but I need to be able to not highlight this blank space.

EDIT: An acceptable alternative would be making the newlines uneccessary. As in, to somehow fix the RichTextBox so that it has always 12 characters on each line, but this is not ideal since I'd like to keep it easy to resize the window.

UPDATE: It seems that decreasing the width of the text column within the XAML has decreased the size of the highlight that goes over the edge, but has not eliminated it completely. I cannot decrease the width any more without causing text to wrap.

Example of Problem

Bassinator
  • 1,682
  • 3
  • 23
  • 50
  • I see one principal problem with these new lines (carriage returns): how do you know that a particular carriage return is there to limit output length and you need to span the selection to the next line, or it is a carriage return that is just part of text, in which case you probably won't span the highlight? – Eduard Malakhov Nov 20 '16 at 20:42
  • Do you stripe out carriage returns and line feeds from the original text before fitting it into the text box? – Eduard Malakhov Nov 20 '16 at 20:53
  • No. There are none. The original text is one contiguous string. I append a `\r` every 12 characters. – Bassinator Nov 20 '16 at 20:57
  • Would enabling WordWrap achieve what you want? – Wazner Nov 22 '16 at 21:29
  • Can you store the positions of \r characters in the string in a list and then exclude them every time you use RichTextBox.Selection.Select()? Maybe if you showed us the code of your algorithm would be helpful – jambonick Nov 23 '16 at 08:17
  • I don't think this is possible. I can strip the characters when I get the contents of the selection for processing, but for displaying, I don't believe you can select discontinuous regions of text. – Bassinator Nov 23 '16 at 13:53
  • Do you need specific features provided by RichTextBox, or can it be a different control? – Maciek Świszczowski Nov 23 '16 at 22:08
  • It *could* be a different control, but it would be a pain in the ass to switch, and who knows if I'll need specific features later on. Things always change – Bassinator Nov 24 '16 at 01:56
  • @dymanoid the WPF RichTextBox is *not* based on the Win32 one already. It uses flow documents instead of RTF. – Lucas Trzesniewski Nov 25 '16 at 11:08
  • @Wazner It turns out you cannot *disable* wordwrap in a `RichTextBox`. Go figure. – Bassinator Nov 25 '16 at 19:45

2 Answers2

6

I have figured out that the problem is not specifically related to the \n character.

I'm still not entirely sure what caused it, but I fixed it like this:

After playing around and noticing that changing the width of the column had an effect on the width of the bad highlighted area, I tried decreasing it away. Problem was, this caused text to wrap.

So I figured, okay, I'll just disable WordWrap for the RichTextBox, right? Wrong. WPF does not allow us to disable this.

I needed a way to achieve the effect of disabling WordWrap, so after doing some reading, I saw that I could set the FlowDocument's Width property to a very large number so that the text would never wrap. Even to do this, I then had to go and disable the scroll bar.

So then, I had the effect of wordwrap disabled and I was able to continue decreasing the width of the RichTextBox until the bad space was invisible, if not gone.

So, problem solved, but I'm still left with one question: Why does Microsoft hate me?

Bassinator
  • 1,682
  • 3
  • 23
  • 50
  • Windows have the line end as `\r\n`, not `\r` not `\n`, you should use `Environment.NewLine` for breaking the word across the lines. Nobody hate you, you've created the problem from nothing – VMAtm Nov 25 '16 at 20:41
  • I can confirm that the problem still exists if I use both characters. That was what I originally was using. – Bassinator Nov 25 '16 at 20:48
-1

Could you try to split the word with '\n'. then loop through each split and pass it to the RichTextBox.Selection.Select().

Something similar to this

Community
  • 1
  • 1
Chochu
  • 1
  • 2