Visual Basic richtextbox - 将特定文本设置为斜体字体样式

发布于 2024-10-24 08:00:06 字数 279 浏览 1 评论 0原文

我创建了一个 Richtextbox,它根据用户输入的变量以及一些基本格式生成文本 - 例如:

name = txtname.text
richtextbox1.text = "Hello my name is " & name & "."

我想要做的是在显示时将名称变量中的文本设置为斜体,如下所示。

你好,我的名字是鲍勃

我能找到的最好的办法是与选择范围有关,但对此没有任何运气。

干杯!

I have created a Richtextbox, which produces text based on user-inputted variables as well as some basic formatting - eg:

name = txtname.text
richtextbox1.text = "Hello my name is " & name & "."

What i want to do is set the text in the name variable in Italics when it is displayed, like this.

Hello my name is Bob.

Best I've been able to find is to do with selection ranges, but not had any luck with that.

Cheers!

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

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

发布评论

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

评论(3

清风无影 2024-10-31 08:00:06

试试这个:

Me.RichTextBox1.Rtf = "{\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fnil\fcharset0 Calibri;}} hello my name is \i Bob\i0 \par}"

如果您使用写字板编写一些示例文本,将其保存为 rtf 格式,然后在记事本中打开该文件,您将得到一些开始。您可以删除写字板添加的一些内容(例如生成它的程序),但看起来您必须至少保留代码页和至少一种字体。

Try this:

Me.RichTextBox1.Rtf = "{\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fnil\fcharset0 Calibri;}} hello my name is \i Bob\i0 \par}"

If you use wordpad to write some sample text, save it in rtf format and then open the file in notepad, you will get something to start with. You can remove some of what wordpad adds (like the program that generated it) but it looks like you have to leave in at least the code page and at least one font.

风透绣罗衣 2024-10-31 08:00:06
Dim BO As New Font("Arial", 12, FontStyle.italic) ' Italic
  richtextbox1.text = "Hello my name is " 
  richtextbox1.selectionfont = BO
  richtextbox1.appendtext(name)

希望这有帮助

Dim BO As New Font("Arial", 12, FontStyle.italic) ' Italic
  richtextbox1.text = "Hello my name is " 
  richtextbox1.selectionfont = BO
  richtextbox1.appendtext(name)

Hope this helps

活泼老夫 2024-10-31 08:00:06

我编写了一个执行此操作的小例程:

Private Sub changeRTF(ByVal strToChange As String, ByRef richTextBox As RichTextBox, ByVal color As Color, Optional ByVal ital As Boolean = False, Optional ByVal bold As Boolean = False, Optional ByVal pointSize As Single = -1)
    richTextBox.SelectionStart = richTextBox.Find(strToChange, RichTextBoxFinds.MatchCase)

    If ital And bold Then
        richTextBox.SelectionFont = New Font(richTextBox.Font, FontStyle.Bold + FontStyle.Italic)
    Else
        If ital Then richTextBox.SelectionFont = New Font(richTextBox.Font, FontStyle.Italic)
        If bold Then richTextBox.SelectionFont = New Font(richTextBox.Font, FontStyle.Bold)
    End If

    richTextBox.SelectionColor = color

    Dim originalFontFamily As FontFamily = richTextBox.SelectionFont.FontFamily
    Dim originalFontStyle As FontStyle = richTextBox.SelectionFont.Style

    If pointSize > 0 Then richTextBox.SelectionFont = New Font(originalFontFamily, pointSize, originalFontStyle)
End Sub

因此,您将创建文本,然后调用 changeRTF("Bob",richtextbox1,color.gold,true)

这段代码的唯一问题是它目前只能找到您正在查找的字符串的第一个存在。我用它来突出显示标题,所以到目前为止这还不是问题(我不重复标题)。

I wrote a little routine that does this:

Private Sub changeRTF(ByVal strToChange As String, ByRef richTextBox As RichTextBox, ByVal color As Color, Optional ByVal ital As Boolean = False, Optional ByVal bold As Boolean = False, Optional ByVal pointSize As Single = -1)
    richTextBox.SelectionStart = richTextBox.Find(strToChange, RichTextBoxFinds.MatchCase)

    If ital And bold Then
        richTextBox.SelectionFont = New Font(richTextBox.Font, FontStyle.Bold + FontStyle.Italic)
    Else
        If ital Then richTextBox.SelectionFont = New Font(richTextBox.Font, FontStyle.Italic)
        If bold Then richTextBox.SelectionFont = New Font(richTextBox.Font, FontStyle.Bold)
    End If

    richTextBox.SelectionColor = color

    Dim originalFontFamily As FontFamily = richTextBox.SelectionFont.FontFamily
    Dim originalFontStyle As FontStyle = richTextBox.SelectionFont.Style

    If pointSize > 0 Then richTextBox.SelectionFont = New Font(originalFontFamily, pointSize, originalFontStyle)
End Sub

So, you would create your text, and then call changeRTF("Bob",richtextbox1,color.gold,true).

The only problem with this code is it currently only finds the first existence of the string you are looking for. I use it to highlight titles so it hasn't been a problem so far (I don't repeat the titles).

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