vb.net - 多色 RichTextBox [英] vb.net - Multicolor RichTextBox

查看:88
本文介绍了vb.net - 多色 RichTextBox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的 Richtextbox 多色中制作一行文本.我尝试了网络上提供的各种实现,并阅读了 SelectedText 和其他主题,但似乎无法按照我想要的方式工作.

I would like to make a line of text in my richtextbox multicolor. I have tried various implementations provided on the web and read up on SelectedText and other topics but can't seem to get it to work the way I would like to.

这是我目前所拥有的

RichTextBox1.Text = "This is black "
RichTextBox1.SelectionFont = New Font("Microsoft Sans Serif", 8.25, FontStyle.Bold)
RichTextBox1.SelectionColor = Color.Green
RichTextBox1.SelectedText = "[BOLD GREEN]"
RichTextBox1.Text = RichTextBox1.Text + " black again"

我想要的颜色作为文字说明.发生的情况是:整行变成绿色,[BOLD GREEN]"出现在文本框的开头而不是内联.

The colors I want are stated as the text. What happens is: the entire line turns green, "[BOLD GREEN]" appears at the beginning of the textbox instead of inline.

我希望它这样读:这是黑色的"为黑色.[BOLD GREEN]"为绿色,black again"为黑色.

I want it to read like this: "this is black" as black. "[BOLD GREEN]" as green and "black again" as black.

推荐答案

您想要达到的目标并不是很清楚.我不确定我对括号格式的理解几乎与您在 Paint 中模拟的图像一样好.但无论如何,这里...

It's not really clear what you're trying to achieve. I'm not sure I understand the bracketed formatting nearly as well as I would an image that you mocked up in Paint. But here goes anyway...

我怀疑您现有的代码存在一些问题.首先是插入新文本时光标的位置.由于插入标记所在的位置,应该在之后第一个片段实际上被插入之前.要解决此问题,您需要手动移动它.

I suspect there are a couple of problems with your existing code. First up is the location of the cursor when you insert new text. What's supposed to come after the first snippet actually gets inserted before it because of where the insertion mark is located. To fix that, you need to move it manually.

您还为代码末尾的 Text 属性分配了一个文本字符串,这不会保留现有的格式信息.我怀疑您要做的最简单的事情是使用 AppendText 方法.

You're also assigning a string of text to the Text property at the end of your code, which does not preserve the existing formatting information. I suspect that the simplest thing for you to do is to use the AppendText method, instead.

最后,我建议使用更简单的重载来创建一种新字体,因为您唯一要更改的是样式.使用它的好处是您不必在代码中硬编码字体的名称和大小,以防以后想更改.

And finally, I recommend using the simpler overload to create a new font, since the only thing you want to change is the style. The advantage of using this instead is that you don't have to hardcode the name and size of the font in your code, in case you want to change it later.

尝试改写您的代码:

' Insert first snippet of text, with default formatting
RichTextBox1.Text = "This is black "

' Move the insertion point to the end of the line
RichTextBox1.Select(RichTextBox1.TextLength, 0)

'Set the formatting and insert the second snippet of text
RichTextBox1.SelectionFont = New Font(RichTextBox1.Font, FontStyle.Bold)
RichTextBox1.SelectionColor = Color.Green
RichTextBox1.AppendText("[BOLD GREEN]")

' Revert the formatting back to the defaults, and add the third snippet of text
RichTextBox1.SelectionFont = RichTextBox1.Font
RichTextBox1.SelectionColor = RichTextBox1.ForeColor
RichTextBox1.AppendText(" black again")

结果如下:

   

   

这篇关于vb.net - 多色 RichTextBox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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