Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用VB.NET编程在富文本框中添加粗体文本_C#_Vb.net_Richtextbox - Fatal编程技术网

C# 如何使用VB.NET编程在富文本框中添加粗体文本

C# 如何使用VB.NET编程在富文本框中添加粗体文本,c#,vb.net,richtextbox,C#,Vb.net,Richtextbox,我有以下代码: print_text.Text = "Patient number: " + ds.Tables("patients").Rows(0).Item(0) print_text.AppendText(Environment.NewLine) print_text.Text = print_text.Text + "Last name: " + ds.Tables("patients").Rows(0).Item(1) print_text.AppendText(Environmen

我有以下代码:

print_text.Text = "Patient number: " + ds.Tables("patients").Rows(0).Item(0)
print_text.AppendText(Environment.NewLine)
print_text.Text = print_text.Text + "Last name: " + ds.Tables("patients").Rows(0).Item(1)
print_text.AppendText(Environment.NewLine)

现在,我以编程方式添加上述数据,效果良好。但是,在上述代码中,我想以粗体字体添加患者编号和姓氏。

使用
RichTextBox.SelectionFont
属性。
查看这些MSDN链接,了解如何执行此操作:和

希望有帮助。
编辑:

  Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Dim len As Integer
    RichTextBox1.Text = "Patient number: " + " 12345"
    RichTextBox1.SelectionStart = 0
    RichTextBox1.SelectionLength = "Patient number".Length
    RichTextBox1.SelectionFont = New Font("Arial", 12, FontStyle.Bold)
    RichTextBox1.SelectionLength = 0
    RichTextBox1.AppendText(Environment.NewLine)
    len = RichTextBox1.Text.Length
    RichTextBox1.AppendText("Last name: " + " ABCD")
    RichTextBox1.SelectionStart = len
    RichTextBox1.SelectionLength = "Last name".Length
    RichTextBox1.SelectionFont = New Font("Arial", 12, FontStyle.Bold)
    RichTextBox1.SelectionLength = 0
End Sub

当使用
RichTextBox
时,为什么不直接使用RTF呢


示例:

Sub Main
    Dim f = new Form()
    Dim print_text = new RichTextBox() With {.Dock = DockStyle.Fill}
    f.Controls.Add(print_text)

    Dim sb = new System.Text.StringBuilder()
    sb.Append("{\rtf1\ansi")
    sb.Append("This number is bold: \b 123\b0 ! Yes, it is...")
    sb.Append("}")
    print_text.Rtf = sb.ToString()

    f.ShowDialog()
End Sub
结果:

Sub Main
    Dim f = new Form()
    Dim print_text = new RichTextBox() With {.Dock = DockStyle.Fill}
    f.Controls.Add(print_text)

    Dim sb = new System.Text.StringBuilder()
    sb.Append("{\rtf1\ansi")
    sb.Append("This number is bold: \b 123\b0 ! Yes, it is...")
    sb.Append("}")
    print_text.Rtf = sb.ToString()

    f.ShowDialog()
End Sub


这样,您还可以轻松地将RTF内容包装到扩展方法中:

Module RtfExtensions

    <Extension()>
    Public Function ToRtf(s As String) As String
        Return "{\rtf1\ansi" + s + "}"
    End Function

    <Extension()>
    Public Function ToBold(s As String) As String
        Return String.Format("\b {0}\b0 ", s)
    End Function

End Module

这适用于选定的文本。我想要的是,我动态添加到富文本框的数据应该是粗体字体,而不是关于动态或静态的。您可以在任何地方使用此属性,但需要适当地使用它。我更新了我的答案,加入了一个简单的例子。请看一看。谢谢。很好很整洁的解决方案,我喜欢。