栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

尝试使用C#SpellCheck类

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

尝试使用C#SpellCheck类

您必须使用WPF
TextBox才能进行拼写检查。您可以使用ElementHost控件将其嵌入Windows窗体表单中。它的工作原理与UserControl非常相似。这是一个您可以直接从工具箱中放下的控件。首先,您需要Project
+ Add
Reference,然后选择WindowsFormsIntegration,System.Design和WPF程序集PresentationCore,Presentationframework和Windowsbase。


将新类添加到您的项目中,然后粘贴以下代码。编译。将SpellBox控件从工具箱的顶部拖放到窗体上。它支持TextChanged事件以及Multiline和WordWrap属性。字体存在困扰,没有简单的方法可以将WF字体映射到WPF字体属性。最简单的解决方法是将窗体的Font设置为WPF的默认“
Segoe UI”。

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 {  }    }    private TextBox box;}

根据普遍的需求,此代码的VB.NET版本避免使用lambda:

imports Systemimports System.ComponentModelimports System.ComponentModel.Design.Serializationimports System.Windowsimports System.Windows.Controlsimports System.Windows.Forms.Integrationimports 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 TextBoxEnd Class


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/430437.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号