在选择文本指针时,不要高亮显示换行字符。

时间:2021-02-04 20:26:18

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.

因此,我有一个RichTextBox(如下所示)它有一个等间距字体,每行必须有相同的字符数。这样做需要在输入框的字符串中每个x字符后插入一个\n换行字符。

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.

问题:当一个词跨越多行时,在第一行的最后一个字符后面会有一个空的空间,我认为这是由\n字符造成的。

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.

如何使RichTextBox.Selection.Select()不突出显示这些空格?我可以在解析时删除字符的选择,但我需要能够不突出这个空白空间。

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.

编辑:一个可接受的选择是使新线路不需要处理。例如,以某种方式修复RichTextBox,使其每行都有12个字符,但这并不理想,因为我希望保持窗口大小的灵活性。

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.

更新:看起来在XAML中减少文本列的宽度已经减少了经过边缘的突出显示的大小,但是还没有完全消除。我不能再减少宽度而不让文本缠绕。

在选择文本指针时,不要高亮显示换行字符。

2 个解决方案

#1


4  

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

我发现这个问题与\n字符并没有特别的关系。

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.

我想,好吧,我将禁用RichTextBox的WordWrap,对吧?错了。WPF不允许我们禁用这个。

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.

我需要一种方法来实现禁用WordWrap的效果,所以在做了一些阅读之后,我看到我可以将FlowDocument的Width属性设置为一个非常大的数字,这样文本就永远不会包装。即使这样,我也不得不禁用滚动条。

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.

然后,我禁用了wordwrap,我可以继续减少RichTextBox的宽度,直到不可见的空间,如果没有消失的话。

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

问题解决了,但我仍然有一个问题:为什么微软讨厌我?

#2


-1  

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

你能用'\n'这个词来拼吗?然后循环每个分割,并将其传递给RichTextBox.Selection.Select()。

Something similar to this

类似于这个

#1


4  

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

我发现这个问题与\n字符并没有特别的关系。

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.

我想,好吧,我将禁用RichTextBox的WordWrap,对吧?错了。WPF不允许我们禁用这个。

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.

我需要一种方法来实现禁用WordWrap的效果,所以在做了一些阅读之后,我看到我可以将FlowDocument的Width属性设置为一个非常大的数字,这样文本就永远不会包装。即使这样,我也不得不禁用滚动条。

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.

然后,我禁用了wordwrap,我可以继续减少RichTextBox的宽度,直到不可见的空间,如果没有消失的话。

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

问题解决了,但我仍然有一个问题:为什么微软讨厌我?

#2


-1  

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

你能用'\n'这个词来拼吗?然后循环每个分割,并将其传递给RichTextBox.Selection.Select()。

Something similar to this

类似于这个