尝试使用 C# SpellCheck 类

发布于 2024-09-29 17:01:05 字数 344 浏览 1 评论 0原文

我正在尝试使用 C# 提供的 SpellCheck 类(在PresentationFramework.dll 中)。 但是,在尝试将拼写绑定到文本框时,我遇到了问题:

SpellCheck.SetIsEnabled(txtWhatever, true);

问题是我的 txtWhatever 属于 System.Windows.Forms 类型,而该函数正在查找的参数是 System.Windows.Controls,并且简单的转换失败。 我也尝试制作这种类型的文本框,但是......不能。 有谁知道如何使用这个 SpellCheck 对象? (MSDN 没有那么有帮助...)

谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

温柔嚣张 2024-10-06 17:01:05

您必须使用 WPF TextBox 才能进行拼写检查。您可以使用 ElementHost 控件将其嵌入到 Windows 窗体窗体中。它的工作原理与 UserControl 非常相似。这是一个可以直接从工具箱中删除的控件。首先,您需要“项目”+“添加引用”并选择“WindowsFormsIntegration”、“System.Design”和 WPF 程序集“PresentationCore”、“PresentationFramework”和“WindowsBase”。

将新类添加到您的项目中并粘贴下面所示的代码。编译。将 SpellBox 控件从工具箱顶部拖放到窗体上。它支持 TextChanged 事件以及 Multiline 和 WordWrap 属性。字体存在一个棘手的问题,没有简单的方法将 WF 字体映射到 WPF 字体属性。最简单的解决方法是将表单的字体设置为“Segoe UI”,这是 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;
}

根据大众的需求,此代码的 VB.NET 版本避免了 lambda:

Imports System
Imports System.ComponentModel
Imports System.ComponentModel.Design.Serialization
Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Forms.Integration
Imports System.Windows.Forms.Design

<Designer(GetType(ControlDesigner))> _
Class SpellBox
    Inherits ElementHost

    Public Sub New()
        box = New TextBox()
        MyBase.Child = box
        AddHandler box.TextChanged, AddressOf box_TextChanged
        box.SpellCheck.IsEnabled = True
        box.VerticalScrollBarVisibility = ScrollBarVisibility.Auto
        Me.Size = New System.Drawing.Size(100, 20)
    End Sub

    Private Sub box_TextChanged(ByVal sender As Object, ByVal e As EventArgs)
        OnTextChanged(EventArgs.Empty)
    End Sub

    Public Overrides Property Text() As String
        Get
            Return box.Text
        End Get
        Set(ByVal value As String)
            box.Text = value
        End Set
    End Property

    <DefaultValue(False)> _
    Public Property MultiLine() As Boolean
        Get
            Return box.AcceptsReturn
        End Get
        Set(ByVal value As Boolean)
            box.AcceptsReturn = value
        End Set
    End Property

    <DefaultValue(False)> _
    Public Property WordWrap() As Boolean
        Get
            Return box.TextWrapping <> TextWrapping.NoWrap
        End Get
        Set(ByVal value As Boolean)
            If value Then
                box.TextWrapping = TextWrapping.Wrap
            Else
                box.TextWrapping = TextWrapping.NoWrap
            End If
        End Set
    End Property

    <DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)> _
    Public Shadows Property Child() As System.Windows.UIElement
        Get
            Return MyBase.Child
        End Get
        Set(ByVal value As System.Windows.UIElement)
            '' Do nothing to solve a problem with the serializer !!
        End Set
    End Property
    Private box As TextBox
End Class

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, PresentationFramework 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;
}

By popular demand, a VB.NET version of this code that avoids the lambda:

Imports System
Imports System.ComponentModel
Imports System.ComponentModel.Design.Serialization
Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Forms.Integration
Imports System.Windows.Forms.Design

<Designer(GetType(ControlDesigner))> _
Class SpellBox
    Inherits ElementHost

    Public Sub New()
        box = New TextBox()
        MyBase.Child = box
        AddHandler box.TextChanged, AddressOf box_TextChanged
        box.SpellCheck.IsEnabled = True
        box.VerticalScrollBarVisibility = ScrollBarVisibility.Auto
        Me.Size = New System.Drawing.Size(100, 20)
    End Sub

    Private Sub box_TextChanged(ByVal sender As Object, ByVal e As EventArgs)
        OnTextChanged(EventArgs.Empty)
    End Sub

    Public Overrides Property Text() As String
        Get
            Return box.Text
        End Get
        Set(ByVal value As String)
            box.Text = value
        End Set
    End Property

    <DefaultValue(False)> _
    Public Property MultiLine() As Boolean
        Get
            Return box.AcceptsReturn
        End Get
        Set(ByVal value As Boolean)
            box.AcceptsReturn = value
        End Set
    End Property

    <DefaultValue(False)> _
    Public Property WordWrap() As Boolean
        Get
            Return box.TextWrapping <> TextWrapping.NoWrap
        End Get
        Set(ByVal value As Boolean)
            If value Then
                box.TextWrapping = TextWrapping.Wrap
            Else
                box.TextWrapping = TextWrapping.NoWrap
            End If
        End Set
    End Property

    <DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)> _
    Public Shadows Property Child() As System.Windows.UIElement
        Get
            Return MyBase.Child
        End Get
        Set(ByVal value As System.Windows.UIElement)
            '' Do nothing to solve a problem with the serializer !!
        End Set
    End Property
    Private box As TextBox
End Class
作业与我同在 2024-10-06 17:01:05

您是否尝试过在尝试拼写检查的实际文本框中设置属性。例如

txtWhatever.SpellCheck.IsEnabled = true;

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

txtWhatever.SpellCheck.IsEnabled = true;
爱你是孤单的心事 2024-10-06 17:01:05

您正在尝试在 WinForms 应用程序上使用专为 WPF 设计的拼写检查组件。他们不相容。

如果您想使用 .NET 提供的拼写检查,则必须使用 WPF 作为您的小部件系统。

如果您想坚持使用 WinForms,则需要第三方拼写检查组件。

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.

新人笑 2024-10-06 17:01:05

此处可以看到基于 WPF 文本框的免费 .NET 拼写检查器,可以在客户端或服务器端使用。它将为您包装文本框,尽管您仍然需要将程序集包含到演示框架等。

完全公开...由您真正编写

Free .NET spell checker based around a WPF text box that can be used client or server side can be seen here. It will wrap the text box for you although you still need the assembly includes to Presentation framework etc.

Full disclosure...written by yours truly

橘和柠 2024-10-06 17:01:05

我需要向 winforms 中的文本框添加背景颜色,以反映设计器中选择的颜色:

public override System.Drawing.Color BackColor
{
    get
    {
        if (box == null) { return Color.White; }
        System.Windows.Media.Brush br = box.Background;
        byte a = ((System.Windows.Media.SolidColorBrush)(br)).Color.A;
        byte g = ((System.Windows.Media.SolidColorBrush)(br)).Color.G;
        byte r = ((System.Windows.Media.SolidColorBrush)(br)).Color.R;
        byte b = ((System.Windows.Media.SolidColorBrush)(br)).Color.B;
        return System.Drawing.Color.FromArgb((int)a, (int)r, (int)g, (int)b);
    }
    set 
    {
        box.Background = new System.Windows.Media.SolidColorBrush(System.Windows.Media.Color.FromArgb(value.A, value.R, value.G, value.B));
    }
}

I needed to add a background colour to the textbox in winforms that reflected the colour selected in the designer:

public override System.Drawing.Color BackColor
{
    get
    {
        if (box == null) { return Color.White; }
        System.Windows.Media.Brush br = box.Background;
        byte a = ((System.Windows.Media.SolidColorBrush)(br)).Color.A;
        byte g = ((System.Windows.Media.SolidColorBrush)(br)).Color.G;
        byte r = ((System.Windows.Media.SolidColorBrush)(br)).Color.R;
        byte b = ((System.Windows.Media.SolidColorBrush)(br)).Color.B;
        return System.Drawing.Color.FromArgb((int)a, (int)r, (int)g, (int)b);
    }
    set 
    {
        box.Background = new System.Windows.Media.SolidColorBrush(System.Windows.Media.Color.FromArgb(value.A, value.R, value.G, value.B));
    }
}
兰花执着 2024-10-06 17:01:05

如果你想启用 TextChanged 事件,请编写如下代码。

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;
        box.TextChanged += new System.Windows.Controls.TextChangedEventHandler(SpellBox_TextChanged);
        this.Size = new System.Drawing.Size(100, 20);
    }

    [Browsable(true)]
    [Category("Action")]
    [Description("Invoked when Text Changes")]
    public new event EventHandler TextChanged;
    protected void SpellBox_TextChanged(object sender, EventArgs e)
    {        
        if (this.TextChanged!=null)
            this.TextChanged(this, e);
    }
    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;
}

If you want to enable the TextChanged Event write the code like this.

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;
        box.TextChanged += new System.Windows.Controls.TextChangedEventHandler(SpellBox_TextChanged);
        this.Size = new System.Drawing.Size(100, 20);
    }

    [Browsable(true)]
    [Category("Action")]
    [Description("Invoked when Text Changes")]
    public new event EventHandler TextChanged;
    protected void SpellBox_TextChanged(object sender, EventArgs e)
    {        
        if (this.TextChanged!=null)
            this.TextChanged(this, e);
    }
    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;
}
幸福还没到 2024-10-06 17:01:05

获取英语单词列表并将其复制到文本文件怎么样?添加参考。然后使用 StreamReader 类根据 textbox.text 分析列表。文本文件中找不到的任何单词都可以设置为突出显示或显示在对话框中,并提供替换或忽略的选项。这是一个霰弹枪建议,有很多缺失的步骤,我已经编程了 2 个月了,但是……无论如何,这就是我要尝试的。我正在制作一个记事本项目(idreamincode.com 上的 rexpad)。希望这有帮助!

what about getting a list of words in the english language and copying that to a text file. add the reference. then use streamreader class to analyze the list against textbox.text. any words not found in the text file could be set to be highlighted or displayed in a dialog box with options to replace or ignore. this is a shotgun suggestion with many missing steps and i am 2 months into programming but....its what im going to attempt anyway. i am making a notepad project (rexpad on idreamincode.com). hope this helped!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文