Simple spelling correction user interface

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:

Relevant API links:

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.

[C#]
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"));
}
[Visual Basic]
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.

[C#]
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);
		}
	}
}
[Visual Basic]
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.

[C#]
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();
}
[Visual Basic]
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