带/ Spelling Error类的文本选择

时间:2015-01-06 00:35:01

标签: wpf vb.net text selection spell-checking

我有一个WPF文本框,我启用了SpellChecking。我已经添加了一个上下文菜单,所以每当出现拼写错误时,用户都可以选择该单词,然后会出现一个下拉菜单:

enter image description here

现在,如果您查看下拉菜单,则会有其他菜单项,例如搜索Google和定义。这些非常重要,应该永远存在。问题是让我说我​​选择这些词:

monky strt

这两个词拼写错误,但我想搜索谷歌。当我继续这样做而不是选择两个单词时,它会选择字符串中的第一个不正确的单词。这就是我的意思:

enter image description here

我选择了两个词:monky strt但monky被选中。 有没有办法选择这两个单词,无论它们是否不正确。但如果我选择整个句子就可以了:

enter image description here

以下是我处理所有这些问题的代码:

Dim index As Integer = 0
    Me.ctrl_TextBox.ContextMenu.Items.Clear()
    'Clearing the existing items
    'Getting the spellcheck suggestions.

    Dim spellingError As SpellingError = Me.ctrl_TextBox.GetSpellingError(Me.ctrl_TextBox.CaretIndex)
    If spellingError IsNot Nothing AndAlso spellingError.Suggestions.Count() >= 1 Then
        For Each suggestion As String In spellingError.Suggestions
            Dim menuItem As New MenuItem()
            menuItem.Header = suggestion
            menuItem.FontWeight = FontWeights.Bold
            menuItem.Command = EditingCommands.CorrectSpellingError
            menuItem.CommandParameter = suggestion
            menuItem.CommandTarget = Me.ctrl_TextBox
            Me.ctrl_TextBox.ContextMenu.Items.Insert(index, menuItem)
            index += 1
        Next
        Dim seperator As New Separator()
        Me.ctrl_TextBox.ContextMenu.Items.Insert(index, seperator)
        index += 1
        'Adding the IgnoreAll menu item
        Dim IgnoreAllMenuItem As New MenuItem()
        IgnoreAllMenuItem.Header = "Ignore All"
        IgnoreAllMenuItem.Command = EditingCommands.IgnoreSpellingError
        IgnoreAllMenuItem.CommandTarget = Me.ctrl_TextBox
        Me.ctrl_TextBox.ContextMenu.Items.Insert(index, IgnoreAllMenuItem)
        index += 1
    Else
        'No Suggestions found, add a disabled NoSuggestions menuitem.
        Dim menuItem As New MenuItem()
        menuItem.Header = "No Suggestions"
        menuItem.IsEnabled = False
        Me.ctrl_TextBox.ContextMenu.Items.Insert(index, menuItem)
        index += 1
    End If

    '.Net 4.0 Supports CustomDictionaries, Option for Adding to dictionary.
    Dim selectionStart As Integer = Me.ctrl_TextBox.GetSpellingErrorStart(Me.ctrl_TextBox.CaretIndex)
    If selectionStart >= 0 Then
        Dim seperator1 As New Separator()
        Me.ctrl_TextBox.ContextMenu.Items.Insert(index, seperator1)
        index += 1
        Dim AddToDictionary As New MenuItem()
        AddToDictionary.Header = "Add to Dictionary"
        'Getting the word to add
        Me.ctrl_TextBox.SelectionStart = selectionStart
        Me.ctrl_TextBox.SelectionLength = Me.ctrl_TextBox.GetSpellingErrorLength(Me.ctrl_TextBox.CaretIndex)
        'Ignoring the added word.
        AddToDictionary.Command = EditingCommands.IgnoreSpellingError
        AddToDictionary.CommandTarget = Me.ctrl_TextBox
        AddHandler AddToDictionary.Click, AddressOf AddToDictionary_Click
        Me.ctrl_TextBox.ContextMenu.Items.Insert(index, AddToDictionary)
        index += 1
    End If

    'Common Edit MenuItems.
    Dim seperator2 As New Separator()
    Me.ctrl_TextBox.ContextMenu.Items.Insert(index, seperator2)
    index += 1
    If ctrl_TextBox.SelectionLength > 0 Then
        'Search
        Dim searchMenuItem As New MenuItem()

        Dim EllipsisSearchString As String = ctrl_TextBox.SelectedText
        If EllipsisSearchString.Length > 16 Then
            EllipsisSearchString = Truncate(ctrl_TextBox.SelectedText, 16) + "..."
        Else
            EllipsisSearchString = Truncate(ctrl_TextBox.SelectedText, 16).ToString
        End If

        searchMenuItem.Header = "Search Google for " + "'" + EllipsisSearchString + "'"
        AddHandler searchMenuItem.Click, AddressOf searchMenuItem_Click
        Me.ctrl_TextBox.ContextMenu.Items.Insert(index, searchMenuItem)
        index += 1

        Dim EllipsisDefineString As String = ctrl_TextBox.SelectedText
        If EllipsisDefineString.Length > 16 Then
            EllipsisDefineString = Truncate(ctrl_TextBox.SelectedText, 16) + "..."
        Else
            EllipsisDefineString = Truncate(ctrl_TextBox.SelectedText, 16).ToString
        End If

        Dim defineMenuItem As New MenuItem()
        defineMenuItem.Header = "Define " + "'" + EllipsisDefineString + "'"
        AddHandler defineMenuItem.Click, AddressOf defineMenuItem_Click
        Me.ctrl_TextBox.ContextMenu.Items.Insert(index, defineMenuItem)
        index += 1

        Dim seperator22 As New Separator()
        Me.ctrl_TextBox.ContextMenu.Items.Insert(index, seperator22)
        index += 1
    End If

    'Cut
    Dim cutMenuItem As New MenuItem()
    cutMenuItem.Command = ApplicationCommands.Cut
    Me.ctrl_TextBox.ContextMenu.Items.Insert(index, cutMenuItem)
    index += 1
    'Copy
    Dim copyMenuItem As New MenuItem()
    copyMenuItem.Command = ApplicationCommands.Copy
    Me.ctrl_TextBox.ContextMenu.Items.Insert(index, copyMenuItem)
    index += 1
    'Paste
    Dim pasteMenuItem As New MenuItem()
    pasteMenuItem.Command = ApplicationCommands.Paste
    Me.ctrl_TextBox.ContextMenu.Items.Insert(index, pasteMenuItem)
    index += 1
    Dim seperator3 As New Separator()
    Me.ctrl_TextBox.ContextMenu.Items.Insert(index, seperator3)
    index += 1
    'Delete
    Dim deleteMenuItem As New MenuItem()
    deleteMenuItem.Command = ApplicationCommands.Delete
    Me.ctrl_TextBox.ContextMenu.Items.Insert(index, deleteMenuItem)
    index += 1
    Dim seperator4 As New Separator()
    Me.ctrl_TextBox.ContextMenu.Items.Insert(index, seperator4)
    index += 1
    'Select All
    Dim selectAllMenuItem As New MenuItem()
    selectAllMenuItem.Command = ApplicationCommands.SelectAll
    Me.ctrl_TextBox.ContextMenu.Items.Insert(index, selectAllMenuItem)
    index += 1

1 个答案:

答案 0 :(得分:0)

所以我终于通过删除来解决这个问题:

'.Net 4.0 Supports CustomDictionaries, Option for Adding to dictionary.
Dim selectionStart As Integer = Me.ctrl_TextBox.GetSpellingErrorStart(Me.ctrl_TextBox.CaretIndex)
If selectionStart >= 0 Then
    Dim seperator1 As New Separator()
    Me.ctrl_TextBox.ContextMenu.Items.Insert(index, seperator1)
    index += 1
    Dim AddToDictionary As New MenuItem()
    AddToDictionary.Header = "Add to Dictionary"
    'Getting the word to add
    Me.ctrl_TextBox.SelectionStart = selectionStart
    Me.ctrl_TextBox.SelectionLength = Me.ctrl_TextBox.GetSpellingErrorLength(Me.ctrl_TextBox.CaretIndex)
    'Ignoring the added word.
    AddToDictionary.Command = EditingCommands.IgnoreSpellingError
    AddToDictionary.CommandTarget = Me.ctrl_TextBox
    AddHandler AddToDictionary.Click, AddressOf AddToDictionary_Click
    Me.ctrl_TextBox.ContextMenu.Items.Insert(index, AddToDictionary)
    index += 1
End If