0

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;
Bridge
  • 29,818
  • 9
  • 60
  • 82
Barry
  • 1
  • 3
  • 5
    Maybe you should link the code so your audience knows what you are talking about. – LarsTech Dec 16 '16 at 21:58
  • Seems fairly straight forward. Start by changing TextBox to RichTextBox and go from there. – LarsTech Dec 18 '16 at 13:27
  • I did that, I changed: – Barry Dec 18 '16 at 13:54
  • box = new RichTextBox(); private RichTextBox box; public override string Text { get { return box.Text; } set { box.Text = value; } } The Get/Set box.Text throws an error. – Barry Dec 18 '16 at 13:57
  • Don't keep the error to yourself if you want help. What does it say? – LarsTech Dec 18 '16 at 14:33
  • Sorry, here is the error ---> "RichTextBox does not contain a definition for 'Text' and no extension method 'Text' accepting a first argument of 'RichTextBox' could be found (are you missing a directive or assembly reference?)" – Barry Dec 19 '16 at 04:48
  • Clearly you are going to have to post your code for us to help fix that. Despite what you wrote, it looks like your box variable is not a RichTextBox control. Did you try to create your own RichTextBox class? – LarsTech Dec 19 '16 at 16:16
  • Where you see TextBox in this code, I changed it to RichTextBox but it fails on the public override string Text Get and Set. The project now crashes the project so I can't post any of the code. If you paste the code above into a project and make the change you should get the same results. – Barry Dec 20 '16 at 03:55
  • See [RichTextBox (WPF) does not have string property “Text”](http://stackoverflow.com/q/957441/719186) – LarsTech Dec 20 '16 at 15:02
  • Lars Tech, that worked! Thank you so much for your help and patience. I am new to C# and Stackoverflow. – Barry Dec 21 '16 at 12:13

0 Answers0