To provide a fully customized or integrated user interface, it is often required to build new dialogs and forms. With TX Spell's SpellingCorrectionUIProvider class, it is very simple to use data binding to achieve that goal without having to deal with events. Data binding in .NET serves the purpose to provide a simple and reliable way to interact with and display data and different states.
The following three articles give an insight into building custom spelling correction forms and dialogs by using the SpellingCorrectionUIProvider class which is provided by the Proofing.TXSpellChecker.SpellingCorrectionUIProvider property.
The corresponding sample projects are contained in the following directories
The sample project SpellingCorrectionUIProvider Simple shows how to set the suggestions in the System.Windows.Forms.ListBox automatically and the Enabled state for each System.Windows.Forms.Button without using additional code. Furthermore, it explains how to connect a TextControl to the SpellingCorrectionUIProvider and handle its misspelled words by using the Change, Delete and Ignore methods.
The source code is contained in the following directories:
There are 3 different places in the project that need to be handled:
1. The Form_Load event, where the SpellingCorrectionUIProvider class is connected to the TextControl and the data bindings are set for Buttons and the suggestion ListBox.
private void Form1_Load(object sender, EventArgs e)
{
...
// connect the TextControl to the TX Spell correction user interface manager
txSpellChecker1.SpellingCorrectionUIProvider.StateManaging.SetCurrent(textControl1);
// automatically add suggestions for the current misspelled word to the ListBox and set its enabled state
listBoxSuggestions.DataBindings.Add(new Binding("DataSource", txSpellChecker1.SpellingCorrectionUIProvider.SuggestionsHandling, "Suggestions"));
listBoxSuggestions.DataBindings.Add(new Binding("Enabled", txSpellChecker1.SpellingCorrectionUIProvider.SuggestionsHandling, "IsSuggestionListEnabled"));
// adding data bindings for the 3 buttons "Change", "Delete" and "Ignore" to set their enabled state
buttonChange.DataBindings.Add(new Binding("Enabled", txSpellChecker1.SpellingCorrectionUIProvider.CorrectionHandling, "IsChangeEnabled"));
buttonDelete.DataBindings.Add(new Binding("Enabled", txSpellChecker1.SpellingCorrectionUIProvider.CorrectionHandling, "IsDeleteEnabled"));
buttonIgnore.DataBindings.Add(new Binding("Enabled", txSpellChecker1.SpellingCorrectionUIProvider.CorrectionHandling, "IsIgnoreEnabled"));
}
Private Sub Form1_Load(sender As Object, e As EventArgs)
...
' connect the TextControl to the TX Spell correction user interface manager
txSpellChecker1.SpellingCorrectionUIProvider.StateManaging.SetCurrent(textControl1)
' automatically add suggestions for the current misspelled word to the ListBox and set its enabled state
listBoxSuggestions.DataBindings.Add(New Binding("DataSource", txSpellChecker1.SpellingCorrectionUIProvider.SuggestionsHandling, "Suggestions"))
listBoxSuggestions.DataBindings.Add(new Binding("Enabled", txSpellChecker1.SpellingCorrectionUIProvider.SuggestionsHandling, "IsSuggestionListEnabled"));
' adding data bindings for the 3 buttons "Change", "Delete" and "Ignore" to set their enabled state
buttonChange.DataBindings.Add(New Binding("Enabled", txSpellChecker1.SpellingCorrectionUIProvider.CorrectionHandling, "IsChangeEnabled"))
buttonDelete.DataBindings.Add(New Binding("Enabled", txSpellChecker1.SpellingCorrectionUIProvider.CorrectionHandling, "IsDeleteEnabled"))
buttonIgnore.DataBindings.Add(New Binding("Enabled", txSpellChecker1.SpellingCorrectionUIProvider.CorrectionHandling, "IsIgnoreEnabled"))
End Sub
2. The StateManaging_PropertyChanged event, that is attached in the Form_Load event and is used to detect whether a misspelled word is currently available or not.
private void Form1_Load(object sender, EventArgs e)
{
...
// attach an event to the property changed event
txSpellChecker1.SpellingCorrectionUIProvider.StateManaging.PropertyChanged +=new PropertyChangedEventHandler(StateManaging_PropertyChanged);
...
}
void StateManaging_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
// select the misspelled word in the TextControl
if (e.PropertyName == "CurrentWordToCorrect")
{
if (txSpellChecker1.SpellingCorrectionUIProvider.StateManaging.CurrentWordToCorrect != null)
{
((TXTextControl.MisspelledWord)txSpellChecker1.SpellingCorrectionUIProvider.StateManaging.CurrentWordToCorrect).Select();
}
else {
// if no misspelled word is available select nothing
textControl1.Select(0, 0);
}
}
}
Private Sub Form1_Load(sender As Object, e As EventArgs)
...
' attach an event to the property changed event
AddHandler txSpellChecker1.SpellingCorrectionUIProvider.StateManaging.PropertyChanged, AddressOf StateManaging_PropertyChanged
...
End Sub
Private Sub StateManaging_PropertyChanged(sender As Object, e As PropertyChangedEventArgs)
' select the misspelled word in the TextControl
If e.PropertyName = "CurrentWordToCorrect" Then
If txSpellChecker1.SpellingCorrectionUIProvider.StateManaging.CurrentWordToCorrect IsNot Nothing Then
DirectCast(txSpellChecker1.SpellingCorrectionUIProvider.StateManaging.CurrentWordToCorrect, TXTextControl.MisspelledWord).[Select]()
Else
' if no misspelled word is available select nothing
textControl1.[Select](0, 0)
End If
End If
End Sub
3. Three Button_Clicked events for each of the Buttons. In each event, the corresponding method is called in the CorrectionHandler class for each Button to change, ignore or delete a misspelled word.
private void buttonChange_Click(object sender, EventArgs e)
{
// change the current misspelled word with the suggestion selected in the ListBox
txSpellChecker1.SpellingCorrectionUIProvider.CorrectionHandling.Change(listBoxSuggestions.Text);
}
private void buttonIgnore_Click(object sender, EventArgs e)
{
// ignore the current misspelled word
txSpellChecker1.SpellingCorrectionUIProvider.CorrectionHandling.Ignore();
}
private void buttonDelete_Click(object sender, EventArgs e)
{
// delete the current misspelled word
txSpellChecker1.SpellingCorrectionUIProvider.CorrectionHandling.Delete();
}
Private Sub buttonChange_Click(sender As Object, e As EventArgs)
' change the current misspelled word with the suggestion selected in the ListBox
txSpellChecker1.SpellingCorrectionUIProvider.CorrectionHandling.Change(listBoxSuggestions.Text)
End Sub
Private Sub buttonIgnore_Click(sender As Object, e As EventArgs)
' ignore the current misspelled word
txSpellChecker1.SpellingCorrectionUIProvider.CorrectionHandling.Ignore()
End Sub
Private Sub buttonDelete_Click(sender As Object, e As EventArgs)
' delete the current misspelled word
txSpellChecker1.SpellingCorrectionUIProvider.CorrectionHandling.Delete()
End Sub
Compared to SpellingCorrectionUIProvider Simple, four new features are added to the SpellingCorrectionUIProvider Advanced sample project:
The source code is contained in the following directories:
There are three new buttons to correct the current misspelled word and all its occurrences: The 'Change All', 'Ignore All' and 'Add To Dict.' button. As previously described in Simple spelling correction user interface, the Enabled states of these Buttons are also bound inside the Form_Load event and the corresponding methods are handled by the Button's Clicked events.
private void Form1_Load(object sender, EventArgs e)
{
...
buttonChangeAll.DataBindings.Add(new Binding("Enabled", txSpellChecker1.SpellingCorrectionUIProvider.CorrectionHandling, "IsChangeAllEnabled"));
buttonIgnoreAll.DataBindings.Add(new Binding("Enabled", txSpellChecker1.SpellingCorrectionUIProvider.CorrectionHandling, "IsIgnoreAllEnabled"));
buttonAddToDict.DataBindings.Add(new Binding("Enabled", txSpellChecker1.SpellingCorrectionUIProvider.CorrectionHandling, "IsAddToDictionaryEnabled"));
}
private void buttonChangeAll_Click(object sender, EventArgs e)
{
...
// change the current misspelled word and all accordances with the suggestion selected in the ListBox
txSpellChecker1.SpellingCorrectionUIProvider.CorrectionHandling.ChangeAll(listBoxSuggestions.Text);
...
}
private void buttonIgnoreAll_Click(object sender, EventArgs e)
{
// ignore the current misspelled word and all accordances
txSpellChecker1.SpellingCorrectionUIProvider.CorrectionHandling.IgnoreAll();
}
private void buttonAddToDict_Click(object sender, EventArgs e)
{
// add the current misspelled word to a user dictionary
txSpellChecker1.SpellingCorrectionUIProvider.CorrectionHandling.AddToDictionary();
}
Private Sub Form1_Load(sender As Object, e As EventArgs)
...
' connect the TextControl to the TX Spell correction user interface manager
buttonChangeAll.DataBindings.Add(New Binding("Enabled", txSpellChecker1.SpellingCorrectionUIProvider.CorrectionHandling, "IsChangeAllEnabled"))
buttonIgnoreAll.DataBindings.Add(New Binding("Enabled", txSpellChecker1.SpellingCorrectionUIProvider.CorrectionHandling, "IsIgnoreAllEnabled"))
buttonAddToDict.DataBindings.Add(New Binding("Enabled", txSpellChecker1.SpellingCorrectionUIProvider.CorrectionHandling, "IsAddToDictionaryEnabled"))
End Sub
Private Sub buttonChangeAll_Click(sender As Object, e As EventArgs)
...
' change the current misspelled word and all accordances with the suggestion selected in the ListBox
txSpellChecker1.SpellingCorrectionUIProvider.CorrectionHandling.ChangeAll(listBoxSuggestions.Text)
...
End Sub
Private Sub buttonIgnoreAll_Click(sender As Object, e As EventArgs)
' ignore the current misspelled word and all accordances
txSpellChecker1.SpellingCorrectionUIProvider.CorrectionHandling.IgnoreAll()
End Sub
Private Sub buttonAddToDict_Click(sender As Object, e As EventArgs)
' add the current misspelled word to a user dictionary
txSpellChecker1.SpellingCorrectionUIProvider.CorrectionHandling.AddToDictionary()
End Sub
In some cases, it is necessary to use more than one dictionary to correct a text. To provide suggestions of one or more specific dictionaries, the user can set the corresponding dictionaries by using the properties of the SuggestionDictionariesHandler class. The recommended way to handle these kind of operations is first getting those dictionaries where the language matches the language of the current misspelled word by calling the SuggestionDictionariesHandler.SuggestionDictionaries property and then adding one or more of these dictionaries to the SuggestionDictionariesHandler.SelectedSuggestionDictionaries property. If this operation causes the creation of new suggestions and the SuggestionsHandler.Suggestions property is bound to a ListBox (see Simple spelling correction user interface), the property and the suggestion ListBox are updated automatically.
In this example, the available suggestion dictionaries are provided by a ComboBox where the DataSource property is bound to the SuggestionDictionariesHandler.SuggestionDictionaries property. Additionally, the ComboBox's Enabled state is bound to the SuggestionDictionariesHandler.IsSuggestionDictionariesEnabled property. If the user changes the selected suggestion dictionary, the corresponding SelectedIndexChanged event adds the new dictionary to the SuggestionDictionariesHandler.SelectedSuggestionDictionaries property.
private void Form1_Load(object sender, EventArgs e)
{
...
// adding data bindings to set the enabled state and automatically add the misspelled words's language depending suggestion dictionaries to the combo box
comboBoxSuggestionDictionaries.DataBindings.Add(new Binding("Enabled", txSpellChecker1.SpellingCorrectionUIProvider.SuggestionDictionariesHandling, "IsSuggestionDictionariesEnabled"));
comboBoxSuggestionDictionaries.DataBindings.Add(new Binding("DataSource", txSpellChecker1.SpellingCorrectionUIProvider.SuggestionDictionariesHandling, "SuggestionDictionaries"));
}
private void comboBoxSuggestionDictionaries_SelectedIndexChanged(object sender, EventArgs e)
{
// Sets the current suggestion dictionaries combo box's selected dictionary as selected dictionary to use for creating suggestions
txSpellChecker1.SpellingCorrectionUIProvider.SuggestionDictionariesHandling.SelectedSuggestionDictionaries = new TXTextControl.Proofing.Dictionary[] { (TXTextControl.Proofing.Dictionary)comboBoxSuggestionDictionaries.SelectedItem };
}
Private Sub Form1_Load(sender As Object, e As EventArgs)
...
' adding data bindings to set the enabled state and automatically add the misspelled words's language depending suggestion dictionaries to the combo box
comboBoxSuggestionDictionaries.DataBindings.Add(New Binding("Enabled", txSpellChecker1.SpellingCorrectionUIProvider.SuggestionDictionariesHandling, "IsSuggestionDictionariesEnabled"))
comboBoxSuggestionDictionaries.DataBindings.Add(New Binding("DataSource", txSpellChecker1.SpellingCorrectionUIProvider.SuggestionDictionariesHandling, "SuggestionDictionaries"))
End Sub
Private Sub comboBoxSuggestionDictionaries_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles comboBoxSuggestionDictionaries.SelectedIndexChanged
' Sets the current suggestion dictionaries combo box's selected dictionary as selected dictionary to use for creating suggestions
txSpellChecker1.SpellingCorrectionUIProvider.SuggestionDictionariesHandling.SelectedSuggestionDictionaries = New TXTextControl.Proofing.Dictionary() {DirectCast(comboBoxSuggestionDictionaries.SelectedItem, TXTextControl.Proofing.Dictionary)}
End Sub
Determining the sentence where the current misspelled word is located could be helpful to correct the word depending on its surrounding. For these cases the PreviewHandler.PreviewSentenceBounds property provides the index and the length of the misspelled word's sentence.
Applied to our example, the sentence extraction is handled by the Format event of the Binding object which connects a RichtTextBox.Text property with the PreviewHandler.PreviewSentenceBounds property. Consequently, on every PreviewSentenceBounds property change, the Format event is raised where finally the misspelled word's sentence text is determined and committed to the RichtTextBox.Text property.
private void Form1_Load(object sender, EventArgs e)
{
...
// adding data binding for the richt text box to get the current misspelled word's sentence.
Binding sentenceBinding = new Binding("Text", txSpellChecker1.SpellingCorrectionUIProvider.PreviewHandling, "PreviewSentenceBounds");
sentenceBinding.Format += new ConvertEventHandler(sentenceBinding_Format);
richTextBoxSentence.DataBindings.Add(sentenceBinding);
}
private void sentenceBinding_Format(object sender, ConvertEventArgs e)
{
if (txSpellChecker1.SpellingCorrectionUIProvider.PreviewHandling.PreviewSentenceBounds != null)
{
e.Value = GetMisspelledWordSentence();
}
else
{
e.Value = "";
}
}
private string GetMisspelledWordSentence()
{
TXTextControl.IFormattedText iftCurrentTextPart = (TXTextControl.IFormattedText)txSpellChecker1.SpellingCorrectionUIProvider.StateManaging.CurrentIFormattedText;
TXTextControl.Paragraph pgMisspelledWordsParagraph = iftCurrentTextPart.Paragraphs.GetItem(((TXTextControl.MisspelledWord)txSpellChecker1.SpellingCorrectionUIProvider.StateManaging.CurrentWordToCorrect).Start);
int[] riPreviewSentenceBounds = txSpellChecker1.SpellingCorrectionUIProvider.PreviewHandling.PreviewSentenceBounds;
return pgMisspelledWordsParagraph.Text.Substring(riPreviewSentenceBounds[0] - (pgMisspelledWordsParagraph.Start - 1), riPreviewSentenceBounds[1]);
}
Private Sub Form1_Load(sender As Object, e As EventArgs)
...
' adding data binding for the richt text box to get the current misspelled word's sentence.
Dim sentenceBinding As New Binding("Text", txSpellChecker1.SpellingCorrectionUIProvider.PreviewHandling, "PreviewSentenceBounds")
AddHandler sentenceBinding.Format, New ConvertEventHandler(AddressOf sentenceBinding_Format)
richTextBoxSentence.DataBindings.Add(sentenceBinding)
End Sub
Private Sub sentenceBinding_Format(ByVal sender As Object, ByVal e As ConvertEventArgs)
If txSpellChecker1.SpellingCorrectionUIProvider.PreviewHandling.PreviewSentenceBounds IsNot Nothing Then
e.Value = GetMisspelledWordSentence()
Else
e.Value = ""
End If
End Sub
Private Function GetMisspelledWordSentence() As String
Dim iftCurrentTextPart As TXTextControl.IFormattedText = DirectCast(txSpellChecker1.SpellingCorrectionUIProvider.StateManaging.CurrentIFormattedText, TXTextControl.IFormattedText)
TXTextControl.Paragraph pgMisspelledWordsParagraph = iftCurrentTextPart.Paragraphs.GetItem(((TXTextControl.MisspelledWord)txSpellChecker1.SpellingCorrectionUIProvider.StateManaging.CurrentWordToCorrect).Start);
Dim pgMisspelledWordsParagraph As TXTextControl.Paragraph = iftCurrentTextPart.Paragraphs.GetItem(DirectCast(txSpellChecker1.SpellingCorrectionUIProvider.StateManaging.CurrentWordToCorrect, TXTextControl.MisspelledWord).Start)
Dim riPreviewSentenceBounds As Integer() = txSpellChecker1.SpellingCorrectionUIProvider.PreviewHandling.PreviewSentenceBounds
Return pgMisspelledWordsParagraph.Text.Substring(riPreviewSentenceBounds(0) - (pgMisspelledWordsParagraph.Start - 1), riPreviewSentenceBounds(1))
End Sub
Sometimes, no suggestions can be created for a misspelled word. In this case, the user needs an interface to correct the word manually.
The SpellingCorrectionUIProvider Advanced sample project provides a TextBox to give the user the opportunity to edit the current misspelled word which is updated inside the box on every StateManager.CurrentWordToCorrect property change. The edited word is applied when the user clicks the 'Change' or 'Change All' button.
private void Form1_Load(object sender, EventArgs e)
{
...
// adding data binding for the text box to get the current misspelled word.
Binding misspelledWordBinding = new Binding("Text", txSpellChecker1.SpellingCorrectionUIProvider.StateManaging, "CurrentWordToCorrect", true, DataSourceUpdateMode.Never);
misspelledWordBinding.Format += new ConvertEventHandler(misspelledWordBinding_Format);
textBoxEdit.DataBindings.Add(misspelledWordBinding);
}
private void misspelledWordBinding_Format(object sender, ConvertEventArgs e)
{
if (txSpellChecker1.SpellingCorrectionUIProvider.StateManaging.CurrentWordToCorrect != null)
{
e.Value = ((TXTextControl.MisspelledWord)txSpellChecker1.SpellingCorrectionUIProvider.StateManaging.CurrentWordToCorrect).Text;
...
}
else
{
e.Value = "";
...
}
}
private void buttonChange_Click(object sender, EventArgs e)
{
if (txSpellChecker1.SpellingCorrectionUIProvider.StateManaging.IsWordToCorrectEditing) {
{
// change the current misspelled word with the text box's text.
txSpellChecker1.SpellingCorrectionUIProvider.CorrectionHandling.Change(textBoxEdit.Text);
}
else
{
// change the current misspelled word with the suggestion selected in the ListBox
txSpellChecker1.SpellingCorrectionUIProvider.CorrectionHandling.Change(listBoxSuggestions.Text);
}
}
Private Sub Form1_Load(sender As Object, e As EventArgs)
...
' adding data binding for the text box to get the current misspelled word.
Dim misspelledWordBinding As New Binding("Text", txSpellChecker1.SpellingCorrectionUIProvider.StateManaging, "CurrentWordToCorrect", True, DataSourceUpdateMode.Never)
AddHandler misspelledWordBinding.Format, New ConvertEventHandler(AddressOf misspelledWordBinding_Format)
textBoxEdit.DataBindings.Add(misspelledWordBinding)
End Sub
Private Sub misspelledWordBinding_Format(ByVal sender As Object, ByVal e As ConvertEventArgs)
If txSpellChecker1.SpellingCorrectionUIProvider.StateManaging.CurrentWordToCorrect IsNot Nothing Then
e.Value = DirectCast(txSpellChecker1.SpellingCorrectionUIProvider.StateManaging.CurrentWordToCorrect, TXTextControl.MisspelledWord).Text
...
Else
e.Value = ""
...
End If
End Sub
Private Sub buttonChange_Click(ByVal sender As Object, ByVal e As EventArgs) Handles buttonChange.Click
If txSpellChecker1.SpellingCorrectionUIProvider.StateManaging.IsWordToCorrectEditing Then
' change the current misspelled word with the text box's text.
txSpellChecker1.SpellingCorrectionUIProvider.CorrectionHandling.Change(textBoxEdit.Text)
Else
' change the current misspelled word with the suggestion selected in the ListBox
txSpellChecker1.SpellingCorrectionUIProvider.CorrectionHandling.Change(listBoxSuggestions.Text)
End If
End Sub
Additionally, the TextBox's TextChanged event calls the CorrectionHandler.CheckCorrectedWord method to check whether the edited TextBox' text is correct or not.
private void textBoxEdit_TextChanged(object sender, EventArgs e)
{
if (txSpellChecker1.SpellingCorrectionUIProvider.StateManaging.Mode != TXTextControl.Proofing.TXSpell.SpellingCorrectionMode.CorrectionCompleted)
{
string message;
if (txSpellChecker1.SpellingCorrectionUIProvider.CorrectionHandling.CheckCorrectedWord(textBoxEdit.Text, out message))
{
// ... do something to show that the text box's text is correct
}
else
{
// ... do something to show that the text box's text is still incorrect
}
}
else
{
// ... the spelling correction is completed. There is no text to check for correctness
}
}
Private Sub textBoxEdit_TextChanged(ByVal sender As Object, ByVal e As EventArgs) Handles textBoxEdit.TextChanged
If txSpellChecker1.SpellingCorrectionUIProvider.StateManaging.Mode <> TXTextControl.Proofing.TXSpell.SpellingCorrectionMode.CorrectionCompleted Then
Dim message As String = String.Empty
If txSpellChecker1.SpellingCorrectionUIProvider.CorrectionHandling.CheckCorrectedWord(textBoxEdit.Text, message) Then
' ... do something to show that the text box's text is correct
Else
' ... do something to show that the text box's text is still incorrect
End If
Else
' ... the spelling correction is completed. There is no text to check for correctness
End If
End Sub
To inform the SpellingCorrectionUIProvider that the user is currently editing the word manually, the StateManager.IsWordToCorrectEditing property is set to true on the first editing operation. To leave the editing mode, the user can press the 'Undo Edit' button where the Proofing.SpellingCorrectionUIProvider.StateManager.IsWordToCorrectEditing property is set to false.
private void Form1_Load(object sender, EventArgs e)
{
...
buttonUndoEdit.DataBindings.Add(new Binding("Enabled", txSpellChecker1.SpellingCorrectionUIProvider.CorrectionHandling, "IsUndoEditEnabled"));
}
private void textBoxEdit_KeyDown(object sender, KeyEventArgs e)
{
// start word editing
txSpellChecker1.SpellingCorrectionUIProvider.StateManaging.IsWordToCorrectEditing = true;
}
private void buttonUndoEdit_Click(object sender, EventArgs e)
{
// abort word editing
txSpellChecker1.SpellingCorrectionUIProvider.StateManaging.IsWordToCorrectEditing = false;
textBoxEdit.Text = ((TXTextControl.MisspelledWord)txSpellChecker1.SpellingCorrectionUIProvider.StateManaging.CurrentWordToCorrect).Text;
buttonIgnore.Focus();
}
Private Sub Form1_Load(sender As Object, e As EventArgs)
...
buttonUndoEdit.DataBindings.Add(New Binding("Enabled", txSpellChecker1.SpellingCorrectionUIProvider.CorrectionHandling, "IsUndoEditEnabled"))
End Sub
Private Sub textBoxEdit_KeyDown(ByVal sender As Object, ByVal e As KeyEventArgs) Handles textBoxEdit.KeyDown
' start word editing
txSpellChecker1.SpellingCorrectionUIProvider.StateManaging.IsWordToCorrectEditing = True
End Sub
Private Sub buttonUndoEdit_Click(ByVal sender As Object, ByVal e As EventArgs) Handles buttonUndoEdit.Click
' abort word editing
txSpellChecker1.SpellingCorrectionUIProvider.StateManaging.IsWordToCorrectEditing = False
textBoxEdit.Text = DirectCast(txSpellChecker1.SpellingCorrectionUIProvider.StateManaging.CurrentWordToCorrect, TXTextControl.MisspelledWord).Text
buttonIgnore.Focus()
End Sub
The sample project Custom SpellCheckDialog shows how to create a fully operational spell check dialog. Its functionality is completely based on the SpellingCorrectionUIProvider methods and properties that were introduced by the previous articles Simple spelling correction user interface and Advanced spelling correction user interface.
The major differences to these samples lie in connecting the TextControl before opening the dialog, handling a preview where to correct manually the current misspelled word, changing the functionality of a button depending on the StateManager.Mode and implementing a Button to open the OptionsDialog.
In the following, the underlying concepts behind these issues are described, but it won't give any further explanation in code details. Therefore, we ask you to explore the Custom SpellCheckDialog sample more closely by yourself.
The principal task of a spell check dialog is to correct the current misspelled words one by one with a correction supporting user interface. But opening such a dialog makes only sense, if the TextControl actually contains misspelled words. To prevent an unnecessary initializing of the spell check dialog, the TextControl should be first connected with the SpellingCorrectionUIProvider to determine its mode. If it is set to SpellingCorrectionMode.NoWordsToCorrectFound, the dialog won't be opened and a MessageBox informs the user that the TextControl does not contain any misspelled words. Whether or not the dialog is shown, the SpellingCorrectionUIProvider should be reset to its initial state by using the StateManager.Cancel method.
Most spell check dialogs provide a preview that gives the user an opportunity to correct the current misspelled word manually. Such an editor usually shows the sentence where the misspelled word is located and highlights it in red color. Additionally, on starting editing inside the preview, the rest of the dialog changes to an editing mode, where most buttons and controls are disabled and the 'Ignore' button is changed to an 'Undo Edit' button.
To implement this behavior, the SpellingCorrectionUIProvider provides the StateManager.IsWordToCorrectEditing property that changes the current mode to SpellingCorrectionMode.WordToCorrectEditing. In this case, bound Button's and Control's are disabled, except the 'Change' and 'Change All' buttons. However, in this example, the Clicked events of these two buttons won't commit the selected suggestion of the suggestion ListBox anymore, but the modified misspelled word from the preview editor as the change parameter. Furthermore, to give the user the chance to cancel misspelled word editing using the preview, the 'Ignore' button is changed to an 'Undo Edit' button, where its Clicked event sets the StateManager.IsWordToCorrectEditing to false. The preview itself is updated on every current misspelled word change or use of the 'Undo Edit' button.
There are to cases where the functionality of a button can be changed. The first one, as described above, is when the 'Ignore' needs to be an 'Undo Edit' button. The other one is when the misspelled word is a duplicated word. In this case the 'Change' button becomes a 'Delete' button and its Clicked event calls the CorrectionHandler.Delete method instead the CorrectionHandler.Change method.
In most spell check dialogs, the user can change some spelling settings, by opening an options dialog. To adopt any changes by these modifications, the connected TextControl must be spell checked again on closing the options dialog. The StateManager.IsOptionsSettings property supports that behavior: Before opening the options dialog, it must be set to true, to save any prior ignore operations. When the dialog is closed, the property is set to false, which leads to a spell check of the TextControl and a resetting of the stored, ignored words.