首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >正在尝试使用C# SpellCheck类

正在尝试使用C# SpellCheck类
EN

Stack Overflow用户
提问于 2010-10-26 22:37:15
回答 7查看 20.6K关注 0票数 22

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

SpellCheck.SetIsEnabled(txtWhatever, true);

问题是我的txtWhatever是System.Windows.Forms类型,这个函数要查找的参数是System.Windows.Controls,简单的转换失败了。我也试过做这种类型的TextBox,但是...不能。有人知道如何使用此SpellCheck对象吗?(MSDN没有那么有帮助...)

谢谢

EN

回答 7

Stack Overflow用户

回答已采纳

发布于 2010-10-27 01:15:40

您必须使用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
票数 57
EN

Stack Overflow用户

发布于 2010-10-26 22:42:44

您是否尝试过在您尝试拼写检查的实际TextBox上设置属性。例如:

txtWhatever.SpellCheck.IsEnabled = true;
票数 0
EN

Stack Overflow用户

发布于 2010-10-26 23:05:16

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

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

如果你想继续使用WinForms,你需要一个第三方的拼写检查组件。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4024798

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档