vb.net - 多色 RichTextBox
我想在我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
目前尚不清楚您想要实现什么目标。我不确定我对括号格式的理解是否和对您在“画图”中模拟的图像的理解一样好。但无论如何……
我怀疑您现有的代码存在一些问题。首先是插入新文本时光标的位置。由于插入标记所在的位置,应该出现在第一个片段之后的内容实际上被插入在它之前。要解决这个问题,您需要手动移动它。
您还在代码末尾将一个文本字符串分配给
Text
属性,这不会保留现有的格式信息。我怀疑您要做的最简单的事情就是使用AppendText
方法,而是。最后,我建议使用更简单的重载来创建新字体,因为您唯一想要改变的是样式。使用它的优点是,您不必在代码中对字体的名称和大小进行硬编码,以防以后想要更改它。
尝试将代码重写为:
结果将如下所示:
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 theAppendText
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:
The result will look like this: