开发者

Is it possible to style text in a richtextbox at design time?

I have a System.Windows.Forms.RichTex开发者_Go百科tBox that I wish to use to display some instructions to my application users.

Is it possible to set some of the text I enter at designtime to be bold?

Or do I have no option but to do it at runtime?


Add a new class to your project and paste the code shown below. Compile. Drop the new control from the top of the tool box onto your form. Select the RichText property and click the button with the dots. That will start Wordpad. Edit your text, type Ctrl+S and close Wordpad. Beware that the Visual Studio designer is non-functional while Wordpad is open.

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


You can certainly create an RTF document in RTF editor (e.g. WordPad), save the file, open it as a text/plain file and copy the RTF document into the RtfText property of your RichTextBox at design time.

But I advise against it. That way, you have a large amount of data in your code and there’s just no point in doing that. Use a resource, that’s what they’re there for, after all. You can bind individual resources to control properties at design time.


I found this link on codeproject to be very useful:

http://www.codeproject.com/KB/miscctrl/richtextboxextended.aspx

It is a fully working rtf-editing control build around the standard .net RichtTextBox control with a good structured code. It shows how to use nearly every available feature of the control.

However, it is written in c# and not vb.net but you should definetly take a look.


@Hans solution worked perfectly. If you want it in C#, you can use the code below:

using System;
using System.Diagnostics;
using System.IO;
using System.ComponentModel;
using System.Drawing.Design;
using System.Windows.Forms;

public class MyRtb : RichTextBox
{
    [Editor(typeof(RtfEditor), typeof(UITypeEditor))]
    public string RichText
    {
        get
        {
            return base.Rtf;
        }
        set
        {
            base.Rtf = value;
        }
    }
}

internal class RtfEditor : UITypeEditor
{
    public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
    {
        return UITypeEditorEditStyle.Modal;
    }

    public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
    {
        string tempFileName = Path.GetTempFileName();
        File.WriteAllText(tempFileName, System.Convert.ToString(value));

        Process process = Process.Start("wordpad.exe", tempFileName);
        
        if (process != null)
        {
            process.WaitForExit();

            value = File.ReadAllText(tempFileName);

            File.Delete(tempFileName);
        }

        return value;
    }
}


I struggled with this for a long while and while Hans' answer was very close, I found that the RTF always reverted to plain text at run time. I found the answer and it has to do with the ForeColor and Font being set by the parent control. When a control is placed inside its parent, some properties are inherited and when the parent controls properties get set, it sets the child controls inherited properties unless they have been explicitly set at design time to be different from the parent. For instance - place a label on a form and leave it as its default properties. Now set the form's font size to a higher value and see that the label's font changed as well.

I have also added the DesignerSerializationVisibility attribute as hidden to avoid the Text, Font, and ForeColor properties from being placed in the designer file at all which could conflict with the RTF as well. Notice that the Set method for these properties is actually not setting the base property of the control - this prevents the base properties from conflicting with the RTF Formatting even when attempting to set them via code:

Imports System.ComponentModel
Imports System.Drawing.Design
Imports System.IO
Imports System.Diagnostics

Public Class MyRtb
    Inherits RichTextBox

    Private _LoadedRTF As String = ""
    <Editor(GetType(RtfEditor), GetType(UITypeEditor))> _
    Public Property RichText() As String
        Get
            Return MyBase.Rtf
        End Get
        Set(ByVal value As String)
            MyBase.Rtf = value
            _LoadedRTF = value
        End Set
    End Property

    <Browsable(False), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)>
    Public Overrides Property Text As String
        Get
            Return MyBase.Text
        End Get
        Set(value As String)
            'MyBase.Text = value
        End Set
    End Property

    <Browsable(False), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)>
    Public Overrides Property Font As Font
        Get
            Return MyBase.Font
        End Get
        Set(value As Font)
            'MyBase.Font = value
        End Set
    End Property

    <Browsable(False), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)>
    Public Overrides Property ForeColor As Color
        Get
            Return MyBase.ForeColor
        End Get
        Set(value As Color)
            'MyBase.ForeColor = value
        End Set
    End Property

    Private Sub Disposed(sender As Object, e As EventArgs) Handles Me.Disposed
        _LoadedRTF = Nothing
    End Sub

    Private Sub RichTextBoxControl_FontChanged(sender As Object, e As EventArgs) Handles Me.FontChanged
        MyBase.Rtf = _LoadedRTF
    End Sub

    Private Sub RichTextBoxControl_ForeColorChanged(sender As Object, e As EventArgs) Handles Me.ForeColorChanged
        MyBase.Rtf = _LoadedRTF
    End Sub
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


Bravo - simple and efficient! Here is also small correction, because argument is long string with spaces so next line contains required quotes:

Process.Start("wordpad.exe", """" & fname & """").WaitForExit() 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜