views:

88

answers:

4

Hi, I am trying to use the SpellCheck class C# provides (in PresentationFramework.dll). But, I am experiencing problems when trying to bind the spelling to my textbox:

SpellCheck.SetIsEnabled(txtWhatever, true);

The problem is that my txtWhatever is of type System.Windows.Forms and the parameter this function is looking for is System.Windows.Controls, and simple converting failed. I also tried to make my TextBox of this type, but... couldn't. Does anyone know how to use this SpellCheck object? (MSDN wasn't that helpful...)

Thanks

A: 

Have you tried just setting the property on the actual TextBox your attempting to spellcheck. e.g.

txtWhatever.SpellCheck.IsEnabled = true;
George
Yes, i tried. System.Windows.Forms doesn't have this property probably. This is exactly my problem...
asaf
Anyway, thanks for the try
asaf
A: 

did some googling, looks like your going to have to roll-your-own. Here is a Code Project article that might help you out, or at least point you in the right direction.

Muad'Dib
+1  A: 

You're trying to use a spell-check component designed for WPF on a WinForms application. They're incompatible.

If you want to use the .NET-provided spell check, you'll have to use WPF as your widget system.

If you want to stick with WinForms, you'll need a third-party spell check component.

Ben Voigt
+2  A: 

You have to use a WPF TextBox to make spell checking work. You can embed one in a Windows Forms form with the ElementHost control. It works pretty similar to a UserControl. Here's a control that you can drop straight from the toolbox. To get started, you need Project + Add Reference and select WindowsFormsIntegration, System.Design and the WPF assemblies PresentationCore, PresetationFramework and WindowsBase.

Add a new class to your project and paste the code shown below. Compile. Drop the SpellBox control from the top of the toolbox onto a form. It supports the TextChanged event and the Multiline and WordWrap properties. There's a nagging problem with the Font, there is no easy way to map a WF Font to the WPF font properties. The easiest workaround for that is to set the form's Font to "Segoe UI", the default for WPF.

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() {
        box = new TextBox();
        base.Child = box;
        box.TextChanged += (s, e) => OnTextChanged(EventArgs.Empty);
        box.SpellCheck.IsEnabled = true;
        box.VerticalScrollBarVisibility = ScrollBarVisibility.Auto;
        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;
}
Hans Passant
I'm trying to implement your solution: What is the (s,e) part of the code means? Because I am getting a compiler error for it?
asaf
It is a lambda expression, VS2008 required. Just delete it and let intellisense add a regular method instead.
Hans Passant
Erm, come to think of it, you have to have VS2008 if you use WPF?
Hans Passant
I'm using VS2005 and the WPF thing works - it is just not yet called WPF... and thanks for the help - the intellisense did its job.
asaf