1
votes

I'm looking at changing my spell checking implementation to use Syncfusion spell checking. I've got it mostly working but have one question and one issue. First my code for the test app. I followed the sample code here and got that working, then I wanted to apply it to multiple textboxes on the same window as that is how it will be in my actual application. Last I tried to activate a custom dictionary.

Here is my XAML code for the app window:

<Window x:Class="SyncfusionWpfApp1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525" Icon="App.ico">
<Grid>
    <StackPanel>
        <TextBox Loaded="tb_Loaded" />
        <TextBox Loaded="tb_Loaded" />
        <TextBox Loaded="tb_Loaded" />
    </StackPanel>
</Grid>

Here is the code behind:

using System.Windows;
using System.Windows.Controls;
using Syncfusion.Windows.Controls;

namespace SyncfusionWpfApp1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        void tb_Loaded(object sender, RoutedEventArgs e)
        {
            var spellChecker = new SfSpellChecker
            {
                CustomDictionaryPath =
                    @"C:\Users\sfaust\Source\Repos\SFTest1\SyncfusionWpfApp1\SyncfusionWpfApp1\bin\Debug\testdictionary.txt"
            };
            var textBoxSpellEditor = new TextBoxSpellEditor((TextBox)sender);
            spellChecker.PerformSpellCheckUsingContextMenu(textBoxSpellEditor);
        }
    }
}

Ok so first question. Once I moved to multiple controls it seemed like it didn't work right unless I create a new spell checker and new IEditorProperties (the TextBoxSpellEditor class) for each textbox and applied it (hence initializing it in loaded above instead of in the window like the example). Well this seems to work it seems pretty inefficient as well. My application will potentially have quite a few text boxes in it as they are inside a tree view of items so I'm a little concerned about the efficiency of having potentially hundreds of spell checkers instantiated (though I haven't tried a stress test so maybe I'm concerned unnecessarily). Is this the correct way to do this?

Second question is more of an issue. I don't see anything indicating the custom dictionary is working. I created the file at the path shown and put a few random "words" in it that it identified (correctly) as misspelled but even after putting them in that file and setting the custom dictionary path property it still identifies them as misspelled. I also don't have the 'AddToDictionary' option on the context menu. I also tried to just set the property without actually creating the file in case it wanted to create the file itself but no change. Last I tried both relative and absolute paths but no change there either. Is there something I am missing on how to activate the custom dictionary?

1

1 Answers

1
votes

Thank you for contacting Syncfusion support.

Query 1 : Once I moved to multiple controls it seemed like it didn't work right unless I create a new spell checker and new IEditorProperties

Instead of creating new instances every time for the SfSpellChecker you can pass the current text box control in the ControlToSpellCheck which is a control property. After that you can call the PerformSpellCheckUsingContextMenu. So, you can obtain the spell checker results with a single instance.

private void OnGotFocus(object sender, RoutedEventArgs e) 
    { 
        TextBox textBox = sender as TextBox; 
        if (this.SpellEditor == null) 
        { 
            SpellEditor = new TextSpellEditor(textBox); 
        } 
        else 
            SpellEditor.ControlToSpellCheck = textBox; 
        SpellChecker.PerformSpellCheckUsingContextMenu(SpellEditor); 
    }

Query 2 : I don't see anything indicating the custom dictionary is working

In SfSpellChecker, we currently don’t have direct support to load the dictionary file from the outside of application. If you want to load resource file different location, we can achieve this by using the reflection and assign the dictionary to the spell checker as per the given code snippet.

For example, if the custom dictionary is present in this location.(D:\CustomDictionary\CustomDict.txt)

Code Example[C#]

Stream fileStream = new FileStream(@"D:\CustomDictionary\CustomDict.txt", FileMode.Open); 
spellChecker.GetType().GetField("checker", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance).SetValue(spellChecker, new SpellCheckerBase(fileStream  ) );