gpt4 book ai didi

c# - 尝试使用 C# SpellCheck 类

转载 作者:IT王子 更新时间:2023-10-29 04:40:57 25 4
gpt4 key购买 nike

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

SpellCheck.SetIsEnabled(txtWhatever, true);

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

谢谢

最佳答案

您必须使用 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

关于c# - 尝试使用 C# SpellCheck 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4024798/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com