将字体样式更改为Richtextbox vb.net

时间:2019-05-19 09:33:29

标签: vb.net

我需要更改字体样式并将其添加到Richtextbox,这是我的代码,但出现一些错误。请帮助

Dim sb = New StringBuilder()

sb.AppendFormat(New Font("IDAutomationHC39M", 12,FontStyle.Regular).AppendLine("SCAN BARCODE: " & txtBarcode.Text)

RichTextBoxPrintCtrl1.Text = sb.ToString()

1 个答案:

答案 0 :(得分:0)

要仅更改RichTextBox中部分文本的字体,您需要搜索所需的文本,然后计算其长度并选择它。此时,您可以将所需的字体应用于SelectionFont属性。

Sub SetBarCodeText(searchText as String)
    Dim len As Integer = searchText.Length
    Dim pos As Integer = RichTextBoxPrintCtrl1.Find(searchText, 0, RichTextBoxFinds.NoHighlight)
    if pos >= 0 Then
        Dim start As Integer = pos
        Dim endpos As Integer = start + len
        RichTextBoxPrintCtrl1.Select(start, endpos - start)
        RichTextBoxPrintCtrl1.SelectionFont = new Font("IDAutomationHC39M", 12, FontStyle.Regular)
    End If
End Sub

,然后使用

调用上述方法
RichTextBoxPrintCtrl1.Text = "SCAN BARCODE: " & txtBarcode.Text
SetBarCodeText("SCAN BARCODE: " & txtBarcode.Text)