TextBox / RichTextBox文本突出显示和选择 [英] TextBox / RichTextBox text highlighting and selection

查看:117
本文介绍了TextBox / RichTextBox文本突出显示和选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个MyClass列表。



这是MyClass的伪定义

{

字符串文本;

int属性;

}



我想在(单个)TextBox中显示它们/ RichTextBox按空格分隔。用户不应该能够编辑文本框中的文本。

如果列表形成句子My Name is Joe(MyClass的不同实例中的每个单词,并且在List中按顺序排列),如果光标在a和m之间的某个位置被点击,应该选择整个单词'name'。在使用右箭头我们应该选择下一个单词。左箭头反之亦然。如果用户使用shift然后多次点击右键,则应选择多个单词。我需要知道从列表中选择了哪些单词,以便我可以触发相同的事件。



此外,根据属性,每个单词的颜色应有所不同。我有预定义的配色方案。例如,1 =红色,2 =蓝色等。

任何帮助都将受到赞赏。

I have a List of MyClass.

Here is a pseudo definition of MyClass
{
string text;
int Attributes;
}

I want to display them in a (single) TextBox / RichTextBox in order separated by space. The user should not be able to edit the text in the text box.
If the list forms the sentence "My Name is Joe"(each word in a different instance of MyClass and is in order in the List), if the cursor is clicked somewhere between a and m in name, the entire word 'name' should get selected. on using right arrow we should select the next word. vice versa for left arrow. If the user uses shift and then taps on right key multiple times, multiple words should get selected. I need to know which words are selected from the list so that I can fire an event regarding the same.

Additionally, each word should be coloured differently depending on the Attributes. I have the colour scheme predefined. For example, 1 = red, 2 = blue etc.
Any help would be appreciated.

推荐答案

您的要求是可能的,但是不是最容易的。我会查找其他richTextBox,它们更容易使用,并可以提供其他功能,如错误下划线。只是一个抬头。



嗯,根据我的理解,你说你不希望它是可编辑的。只需将只读属性设置为 true



着色部分很难,因为我知道你只能通过选择它,着色它然后取消选择它来改变所需文本部分的颜色。哪个真的很难看。我想知道用多个RichTextBox制作自己的自定义控件是否更容易,所以你可以单独设置每个RichTextBox的颜色而不选择文本。



回到选择部分,如果你要保留一个richtextbox,你会想要保存包含StartIndex和Length的每个 MyClass 的信息,所以当用户点击时你可以查看 MyClass 的列表,看看它是否包含光标。这个算法也可以用来选择整个单词,这实际上看起来像一个简单的概念。



这大致涵盖了我想你所说的,除了换档选择,如果你让其他概念先发挥作用,可能很容易理解。以下是一些文章,深入介绍RichTextBox着色单独文本。



RichTextBox中使用C#的多个彩色文本 [ ^ ]



如何在使用SPLIT后更改RichTextBox上的字体颜色 [ ^ ]



希望这会有所帮助。
What your asking is possible, but not the easiest. I would look up other richTextBoxes, they are easier to work with and can provide other feutures like error underlining. Just a heads up.

Well, from my understanding you said you don't want it to editable. Simply just set the Read-Only property to true.

The coloring part is difficult, because I know that you can only change the color of a desired text part by Selecting it, Coloring it, then unselecting it. Which can be really ugly. I'm wondering if it would be easier to make your own custom control with multiple RichTextBoxes so you can just set the color of each one separately without selecting the text.

Back to the selecting part, if you were to keep one richtextbox, you're going to want to keep information for each MyClass containing a StartIndex and Length, so when the user clicks you can go through your list of MyClass's and see if it contains the cursor. This algorithm could also you used to select the whole word, that actually seems like a easy concept.

That roughly covers, what I thought you said, except for the shift-selecting which probably would be easy to figure out if you got the other concepts working first. Here are some articles explaining further in depth on RichTextBox coloring seperate texts.

Multiple Colored Texts in RichTextBox using C#[^]

How to Change Font Color on RichTextBox after USING SPLIT[^]

Hope this helps.


我建议看看这个:



http://www.c-sharpcorner.com/UploadFile/mahesh/wpf-richtextbo x / [ ^ ]



他在代码和xaml中进行格式化操作。
I would suggest looking at this:

http://www.c-sharpcorner.com/UploadFile/mahesh/wpf-richtextbox/[^]

He does formatting manipulation in both code and in xaml.


这篇关于TextBox / RichTextBox文本突出显示和选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆