vb.net - 多色 RichTextBox

发布于 2024-10-24 18:25:46 字数 527 浏览 1 评论 0原文

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

到目前为止,这是

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]”出现在文本框的开头而不是内联。

我希望它读起来像这样:“这是黑色的”黑色。 “[BOLD GREEN]”为绿色,“black Again”为黑色。

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.

Here is what I have so far

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"

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.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

欲拥i 2024-10-31 18:25:46

目前尚不清楚您想要实现什么目标。我不确定我对括号格式的理解是否和对您在“画图”中模拟的图像的理解一样好。但无论如何……

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

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

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

尝试将代码重写为:

' 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")

结果将如下所示:

   sample RichTextBox with formatted text

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.

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.

Try rewriting your code to this instead:

' 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")

The result will look like this:

   sample RichTextBox with formatted text

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文