我正在尝试使用C#提供的SpellCheck类(在PresentationFramework.dll中)。 但是,我在尝试将拼写绑定到文本框时遇到问题:
SpellCheck.SetIsEnabled(txtWhatever, true);
问题是我的txtWake类型System.Windows.Forms和该函数正在寻找的参数是System.Windows.Controls,简单转换失败。 我也尝试制作这种类型的TextBox,但是......不能。 有谁知道如何使用这个SpellCheck对象? (MSDN没那么有用......)
由于
答案 0 :(得分:51)
您必须使用WPF TextBox才能进行拼写检查。您可以使用ElementHost控件在Windows窗体表单中嵌入一个。它的工作方式与UserControl非常相似。这是一个可以直接从工具箱中删除的控件。首先,您需要Project + Add Reference并选择WindowsFormsIntegration,System.Design和WPF程序集PresentationCore,PresentationFramework和WindowsBase。
在项目中添加一个新类并粘贴下面显示的代码。编译。将SpellBox控件从工具箱顶部拖放到表单上。它支持TextChanged事件以及Multiline和WordWrap属性。 Font有一个令人烦恼的问题,没有简单的方法将WF字体映射到WPF字体属性。最简单的解决方法是将表单的Font设置为“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
答案 1 :(得分:0)
您是否尝试过在尝试拼写检查的实际TextBox上设置属性。例如
txtWhatever.SpellCheck.IsEnabled = true;
答案 2 :(得分:0)
您正尝试在WinForms应用程序上使用为WPF设计的拼写检查组件。他们是不相容的。
如果要使用.NET提供的拼写检查,则必须使用WPF作为窗口小部件系统。
如果你想坚持使用WinForms,你需要一个第三方拼写检查组件。
答案 3 :(得分:0)
可以看到基于WPF文本框的可用于客户端或服务器端的免费.NET拼写检查程序here。它将为您包装文本框,尽管您仍然需要将程序集包含到Presentation框架等。
完全披露......由您真正撰写
答案 4 :(得分:0)
我需要在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));
}
}
答案 5 :(得分:0)
如果您想启用 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;
}
答案 6 :(得分:-1)
如何获取英语单词列表并将其复制到文本文件中。添加引用。然后使用streamreader类来分析textbox.text的列表。任何在文本文件中找不到的单词都可以设置为在对话框中突出显示或显示,并带有要替换或忽略的选项。这是一个霰弹枪的建议,有许多缺失的步骤,我在编程2个月,但....它无论如何我将尝试。我正在做一个记事本项目(idxincode.com上的rexpad)。希望这有帮助!