如何添加在格式文本框一个大胆的文本编程方式使用VB.NET [英] How to add a bold text in Rich TextBox programatically using VB.NET

查看:265
本文介绍了如何添加在格式文本框一个大胆的文本编程方式使用VB.NET的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的代码:

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)

现在上述数据我正在编程方式添加和它工作正常。然而,在上面的代码我想添加病人数粗体。

Now the above data i am adding programatically and it works fine. However in the above code i want to add Patient number and Last name in bold font.

推荐答案

在使用的RichTextBox ,为什么不使用RTF?

When using a RichTextBox, why not just use 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

结果:

MSDN

这样,你也可以很容易地包住RTF东西到扩展方法:

This way, you can also easily wrap the RTF stuff into extension methods:

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

和使用它像

Dim text = "This number is bold: " + "123".ToBold() + "! Yes, it is..."
print_text.Rtf = text.ToRtf()

这篇关于如何添加在格式文本框一个大胆的文本编程方式使用VB.NET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆