2010-10-26 23 views
15

我想使用SpellCheck類C#提供(在PresentationFramework.dll中)。 但是,我想拼寫綁定到我的文本框時遇到的問題:試圖使用C#SpellCheck類

SpellCheck.SetIsEnabled(txtWhatever, true); 

的問題是,我txtWhatever的類型是System.Windows.Forms的,並且該功能尋找的參數是System.Windows .Controls和簡單的轉換失敗。 我也試圖讓我的文本框這種類型,但...不能。 有誰知道如何使用這個SpellCheck對象? (MSDN是不是有幫助...)

感謝

回答

42

您必須使用WPF文本框進行拼寫檢查工作。您可以使用ElementHost控件將其嵌入到Windows窗體表單中。它和UserControl非常相似。這是一個可以直接從工具箱中刪除的控件。要開始,您需要Project + Add Reference並選擇WindowsFormsIntegration,System.Design和WPF程序集PresentationCore,PresentationFramework和WindowsBase。

向您的項目中添加一個新類並粘貼下面顯示的代碼。編譯。將工具箱頂部的SpellBox控件拖放到表單上。它支持TextChanged事件和Multiline和WordWrap屬性。 Font有一個煩人的問題,沒有簡單的方法將WF Font映射到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版本,避免拉姆達:

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 
+0

我試圖實現你的解決方案:什麼是代碼的(s,e)部分意味着什麼?因爲我得到一個編譯器錯誤呢? – Assaf 2010-10-27 07:14:49

+0

這是一個lambda表達式,需要VS2008。只要刪除它,讓intellisense添加一個常規的方法。 – 2010-10-27 07:21:49

+0

呃,想一想,如果你使用WPF,你必須擁有VS2008嗎? – 2010-10-27 07:22:34

0

你試過只設置實際文本框您嘗試拼寫檢查的屬性。例如

txtWhatever.SpellCheck.IsEnabled = true; 
+0

是的,我試過了。 System.Windows.Forms可能沒有這個屬性。這正是我的問題... – Assaf 2010-10-26 14:49:11

+0

無論如何,謝謝你的嘗試 – Assaf 2010-10-26 14:49:57

0

您試圖在WinForms應用程序中使用爲WPF設計的拼寫檢查組件。它們不兼容。

如果你想使用.NET提供的拼寫檢查,你必須使用WPF作爲你的widget系統。

如果你想堅持使用WinForms,你需要第三方拼寫檢查組件。

-1

怎麼樣得到單詞列表中的英語和複製該到一個文本文件。添加參考。然後使用streamreader類根據textbox.text分析列表。在文本文件中找不到的任何單詞可以設置爲突出顯示或顯示在帶有替換或忽略選項的對話框中。這是一個霰彈槍的建議,許多缺失的步驟,我是2個月的編程,但......它的即時嘗試無論如何。我正在做一個記事本項目(idreamincode.com上的rexpad)。希望這有助於!

+0

這不僅僅是一個回答而是一個評論。你讀過接受的答案嗎? – 2012-10-10 20:24:25

0

基於WPF文本框可以使用客戶端或服務器端的免費.NET拼寫檢查器可以看到here。它將爲您包裝文本框,雖然您仍然需要包含到演示文稿框架等的組件。

完全披露...通過你的書面真正

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)); 
    } 
}