Convert Spellbox to a RichTextBox

113 views Asked by At

Hans Passant has code that adds a textbox based spellbox for spell checking in WinForms app. It works great but I would like to know if it could be converted to a RichTextBox so that I could add bolding, color, etc.

using System;
using System.ComponentModel;
using System.ComponentModel.Design.Serialization;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Forms.Integration;
using System.Windows.Forms.Design;

[Designer(typeof(ControlDesigner))]
//[DesignerSerializer("System.Windows.Forms.Design.ControlCodeDomSerializer, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", "System.ComponentModel.Design.Serialization.CodeDomSerializer, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
class SpellBox : ElementHost
{
    public SpellBox()
    {
     //   string templatePath = System.Environment.GetEnvironmentVariable("USERPROFILE") + "\\Documents\\AviationMaintenanceLogger\\Templates\\";
        box = new TextBox();
        base.Child = box;
        box.TextChanged += (s, e) => OnTextChanged(EventArgs.Empty);
        Uri lex_file = new Uri(System.Environment.GetEnvironmentVariable("USERPROFILE") + "\\Documents\\AviationMaintenanceLogger\\Templates\\AML.lex");
        box.SpellCheck.CustomDictionaries.Add(lex_file);
        box.SpellCheck.IsEnabled = true;
        box.VerticalScrollBarVisibility = ScrollBarVisibility.Auto;
        this.WordWrap = true;
        this.Size = new System.Drawing.Size(100, 20);

    }
    public override string Text
    {
        get { return box.Text; }
        set { box.Text = value; }
    }
    [DefaultValue(false)]
    public bool Multiline
    {
        get { return box.AcceptsReturn; }
        set { box.AcceptsReturn = value; }
    }
    [DefaultValue(false)]
    public bool WordWrap
    {
        get { return box.TextWrapping != TextWrapping.NoWrap; }
        set { box.TextWrapping = value ? TextWrapping.Wrap : TextWrapping.NoWrap; }
    }
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
    public new System.Windows.UIElement Child
    {
        get { return base.Child; }
        set { /* Do nothing to solve a problem with the serializer !! */ }
    }

    private TextBox box;
}

This is the new code that works:

using System;
using System.ComponentModel;
using System.ComponentModel.Design.Serialization;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Forms.Integration;
using System.Windows.Forms.Design;
using System.Windows.Documents;
using System.Drawing;

[Designer(typeof(ControlDesigner))]
//[DesignerSerializer("System.Windows.Forms.Design.ControlCodeDomSerializer, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", "System.ComponentModel.Design.Serialization.CodeDomSerializer, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
class SpellBoxNew : ElementHost
{
    public SpellBoxNew()
    {
        //   string templatePath = System.Environment.GetEnvironmentVariable("USERPROFILE") + "\\Documents\\AviationMaintenanceLogger\\Templates\\";
        box = new RichTextBox();
        base.Child = box;
        box.IsReadOnly = false;
        box.TextChanged += (s, e) => OnTextChanged(EventArgs.Empty);
        Uri lex_file = new Uri(System.Environment.GetEnvironmentVariable("USERPROFILE") + "\\Documents\\AviationMaintenanceLogger\\Templates\\AML.lex");
        box.SpellCheck.CustomDictionaries.Add(lex_file);
        box.SpellCheck.IsEnabled = true;
        box.VerticalScrollBarVisibility = ScrollBarVisibility.Auto;
        //this.Font = new Font("Arial", 24,FontStyle.Bold);
        // this.WordWrap = true;
        this.Multiline = true;
        this.Size = new System.Drawing.Size(100, 20);
    }

    [DefaultValue(false)]
    public override string Text
    {
        get
        {
           // string richText = new TextRange(transcriberArea.Document.ContentStart, transcriberArea.Document.ContentEnd).Text;
            string richText = new TextRange(box.Document.ContentStart, box.Document.ContentEnd).Text;
            return richText;
        }
        set
        {
            box.Document.Blocks.Clear();
            box.Document.Blocks.Add(new System.Windows.Documents.Paragraph(new Run(value)));
        }
    }


    public bool Multiline
    {
        get { return box.AcceptsReturn; }
        set { box.AcceptsReturn = value; }
    }
    [DefaultValue(false)]

    [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
    public new System.Windows.UIElement Child
    {
        get { return base.Child; }
        set { /* Do nothing to solve a problem with the serializer !! */ }
    }


    private RichTextBox box;
0

There are 0 answers