vb.net Visual Basic Richtextbox - 将特定文本设置为斜体字体样式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5362957/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 15:22:26  来源:igfitidea点击:

Visual Basic richtextbox - setting specific text to Italic font style

vb.netvisual-studio-2010richtextboxtext-formatting

提问by Lwkd

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

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

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.

我想要做的是在显示时将 name 变量中的文本设置为斜体,就像这样。

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!

干杯!

回答by Judah Sali

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.

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

回答by Dave A

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

希望这可以帮助

回答by APrough

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).

因此,您将创建文本,然后调用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).

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