我有一个 System.Windows.Forms.RichTextBox,我希望用它来向我的应用程序用户显示一些说明。
是否可以将我在设计时输入的某些文本设置为粗体?
还是我别无选择,只能在运行时执行?
我有一个 System.Windows.Forms.RichTextBox,我希望用它来向我的应用程序用户显示一些说明。
是否可以将我在设计时输入的某些文本设置为粗体?
还是我别无选择,只能在运行时执行?
向您的项目添加一个新类并粘贴如下所示的代码。编译。将新控件从工具箱顶部拖放到表单上。选择 RichText 属性并单击带有点的按钮。这将启动写字板。编辑文本,键入 Ctrl+S 并关闭写字板。请注意,当 Wordpad 打开时,Visual Studio 设计器不起作用。
Imports System.ComponentModel
Imports System.Drawing.Design
Imports System.IO
Imports System.Diagnostics
Public Class MyRtb
Inherits RichTextBox
<Editor(GetType(RtfEditor), GetType(UITypeEditor))> _
Public Property RichText() As String
Get
Return MyBase.Rtf
End Get
Set(ByVal value As String)
MyBase.Rtf = value
End Set
End Property
End Class
Friend Class RtfEditor
Inherits UITypeEditor
Public Overrides Function GetEditStyle(ByVal context As ITypeDescriptorContext) As UITypeEditorEditStyle
Return UITypeEditorEditStyle.Modal
End Function
Public Overrides Function EditValue(ByVal context As ITypeDescriptorContext, ByVal provider As IServiceProvider, ByVal value As Object) As Object
Dim fname As String = Path.Combine(Path.GetTempPath, "text.rtf")
File.WriteAllText(fname, CStr(value))
Process.Start("wordpad.exe", fname).WaitForExit()
value = File.ReadAllText(fname)
File.Delete(fname)
Return value
End Function
End Class
您当然可以在 RTF 编辑器(例如 WordPad)中创建一个 RTF 文档,保存文件,将其作为文本/纯文本文件打开,然后在设计时将 RTF 文档复制到RtfText
您的属性中。RichTextBox
但我建议不要这样做。这样一来,您的代码中就有大量数据,而这样做毫无意义。毕竟,使用资源就是他们的目的。您可以在设计时将各个资源绑定到控制属性。
我发现 codeproject 上的这个链接非常有用:
http://www.codeproject.com/KB/miscctrl/richtextboxextended.aspx
它是一个完全工作的 rtf 编辑控件,围绕标准 .net RichtTextBox 控件构建,具有良好的结构化代码。它展示了如何使用控件的几乎所有可用功能。
但是,它是用 c# 而不是 vb.net 编写的,但您一定要看看。
Bravo - 简单高效!这也是一个小的修正,因为参数是带有空格的长字符串,所以下一行包含必需的引号:
Process.Start("wordpad.exe", """" & fname & """").WaitForExit()