# Checking Multiple Controls

> Share one TXSpellChecker resource between several WPF TextControls in XAML, and check TextControls or TextBoxes in one dialog with SpellCheckDialogClosing.

- **Product:** TX Spell .NET for WPF
- **Section:** Documentation
- **Kind:** Topic
- **Path:** TX Spell .NET for WPF > Spell Checking > Checking Multiple Controls
- **2963 words**
- **Web URL:** https://docs.textcontrol.com/tx-spell-dotnet-wpf/spell-checking/multiple-controls/
- **LLMs URL:** https://docs.textcontrol.com/tx-spell-dotnet-wpf/spell-checking/multiple-controls/llms.txt
- **LLMs-Full URL:** https://docs.textcontrol.com/tx-spell-dotnet-wpf/spell-checking/multiple-controls/llms-full.txt

---

A window with several editors -- a letter and its cover note, the fields of a record, the panes of a comparison -- needs only one spell checker. A single `TXSpellChecker` serves any number of controls: the dictionaries are loaded once, a word added to the user dictionary in one editor is accepted in all others at once, and memory use stays that of one instance. It covers checking as the user types in several `TextControl` instances, and one spell check dialog that works through several controls in turn.

### Share One Spell Checker Between TextControls

The connection is made on the editor's side. Each `TextControl` has a [SpellChecker](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol-wpf/textcontrol/spellchecker-property/llms-full.txt) property that holds the key of a spell checker resource, and you give every editor the same key. TX Text Control looks for the key in the resources of the `TextControl` and of its parents, so one resource of the window serves every editor in it. There is nothing to switch when the user moves from one editor to the next.

1. Declare one [TXSpellChecker](https://docs.textcontrol.com/tx-spell-dotnet-wpf/reference/txtextcontrol-wpf-proofing/txspellchecker/llms-full.txt) in the resources of the window:

    ![XAML of a window that maps the spell prefix to TXTextControl.WPF.Proofing in the TXSpell.WPF assembly and declares a spell:TXSpellChecker with the key txSpellChecker1 in Window.Resources.](https://docs.textcontrol.com/assets/img/tx-spell-dotnet-wpf/xaml-proofing-namespace.8c42d1ed.webp)
2. Set `SpellChecker="txSpellChecker1"` and `IsSpellCheckingEnabled="True"` on each `TextControl`:

    ![XAML of four TextControls, each with IsSpellCheckingEnabled set to True and SpellChecker set to txSpellChecker1.](https://docs.textcontrol.com/assets/img/tx-spell-dotnet-wpf/xaml-four-textcontrols.f7dfcd88.webp)

In text, the markup of the two steps reads:

```xml
<Window.Resources>
    <spell:TXSpellChecker x:Key="txSpellChecker1" />
</Window.Resources>
<UniformGrid Rows="2" Columns="2">
    <tx:TextControl x:Name="textControl1" SpellChecker="txSpellChecker1" IsSpellCheckingEnabled="True" />
    <tx:TextControl x:Name="textControl2" SpellChecker="txSpellChecker1" IsSpellCheckingEnabled="True" />
    <tx:TextControl x:Name="textControl3" SpellChecker="txSpellChecker1" IsSpellCheckingEnabled="True" />
    <tx:TextControl x:Name="textControl4" SpellChecker="txSpellChecker1" IsSpellCheckingEnabled="True" />
</UniformGrid>
```

The `spell` and `tx` prefixes are mapped as in [As-You-Type Checking](https://docs.textcontrol.com/tx-spell-dotnet-wpf/spell-checking/as-you-type/#connect-the-spell-checker-in-xaml). Each editor now underlines its misspelled words and shows the same context menu, backed by the same dictionaries and user dictionaries:

![Four TextControls in one window with red underlines, and the context menu of a misspelled word with suggestions, Ignore, Ignore All, Add to Dictionary, Spell Check Dialog and Options.](https://docs.textcontrol.com/assets/img/tx-spell-dotnet-wpf/four-textcontrols-context.bfb63dae.webp)

### Check Several TextControls in One Dialog

The [SpellCheckDialog](https://docs.textcontrol.com/tx-spell-dotnet-wpf/reference/txtextcontrol-wpf-proofing/spellcheckdialog/llms-full.txt) of TX Spell .NET checks one control at a time. To have it continue with the next editor instead of closing, handle [SpellCheckDialogClosing](https://docs.textcontrol.com/tx-spell-dotnet-wpf/reference/txtextcontrol-wpf-proofing/txspellchecker/spellcheckdialogclosing-event/llms-full.txt). It fires when the dialog is about to close, either because the user closed it or because the current control has no more misspelled words. `txSpellChecker1` is the resource of the window, which the code-behind gets with `FindResource`, as [As-You-Type Checking](https://docs.textcontrol.com/tx-spell-dotnet-wpf/spell-checking/as-you-type/#use-the-spell-checker-in-code) shows.

1. Attach the handler once, after `FindResource` in the constructor:

```csharp
txSpellChecker1.SpellCheckDialogClosing += txSpellChecker1_SpellCheckDialogClosing;
```

1. Open the dialog for the first editor and remember which editor it checks:

```csharp
private TXTextControl.WPF.TextControl[] textControls;
private TXTextControl.WPF.TextControl curTextControl;

private void buttonSpelling_Click(object sender, RoutedEventArgs e)
{
    textControls = new[] { textControl1, textControl2, textControl3, textControl4 };
    curTextControl = textControls[0];
    txSpellChecker1.SpellCheckDialog.Show(curTextControl);
}
```

1. Handle `SpellCheckDialogClosing`. If the current editor is done and another follows, hand it to the dialog and cancel the closing:

```csharp
private void txSpellChecker1_SpellCheckDialogClosing(object sender, TXTextControl.WPF.Proofing.SpellCheckDialogClosingEventArgs e)
{
    // A word is still waiting: the user closed the dialog early
    if (e.StateManager.NextMisspelledWord != null) return;

    e.Cancel = SetNextTextControl(e.StateManager);
}

private bool SetNextTextControl(TXTextControl.WPF.Proofing.SpellCheckDialogStateManager stateManager)
{
    int index = Array.IndexOf(textControls, curTextControl);

    // The last editor is done: let the dialog close
    if (index == textControls.Length - 1) return false;

    curTextControl = textControls[index + 1];
    stateManager.SetNext(curTextControl);
    return true;
}
```

1. Run the application and click the button. The dialog works through the first editor, then continues with the second, and closes after the last.

The code works as follows:

- [StateManager](https://docs.textcontrol.com/tx-spell-dotnet-wpf/reference/txtextcontrol-wpf-proofing/spellcheckdialogclosingeventargs/statemanager-property/llms-full.txt), a [SpellCheckDialogStateManager](https://docs.textcontrol.com/tx-spell-dotnet-wpf/reference/txtextcontrol-wpf-proofing/spellcheckdialogstatemanager/llms-full.txt), tells the handler where the dialog stands and lets it decide what comes next.
- [NextMisspelledWord](https://docs.textcontrol.com/tx-spell-dotnet-wpf/reference/txtextcontrol-wpf-proofing/spellcheckdialogstatemanager/nextmisspelledword-property/llms-full.txt) is the word the dialog would show next. If it is not `null`, the check was not finished, so the user closed the dialog and the handler lets it close.
- [SetNext](https://docs.textcontrol.com/tx-spell-dotnet-wpf/reference/txtextcontrol-wpf-proofing/spellcheckdialogstatemanager/setnext-method/llms-full.txt) with a `TextControl` makes that editor the next one the dialog checks.
- Setting [Cancel](https://docs.textcontrol.com/tx-spell-dotnet-wpf/reference/txtextcontrol-wpf-proofing/spellcheckdialogclosingeventargs/cancel-property/llms-full.txt) to `true` keeps the dialog open. The user sees one dialog for all editors.

The dialog checks every text part of each editor, headers, footers and text frames included. The *TextControl\\SpellCheckDialog on multiple controls* sample of the setup is built this way:

![One Spelling dialog over four TextControls, showing a misspelled word of the first editor.](https://docs.textcontrol.com/assets/img/tx-spell-dotnet-wpf/textcontrol-multi-dialog.69697861.webp)

### Check Several TextBoxes in One Dialog

Without a connected `TextControl`, the spell checker isn't initialized for you: call its [Create](https://docs.textcontrol.com/tx-spell-dotnet-wpf/reference/txtextcontrol-wpf-proofing/txspellchecker/create-method/llms-full.txt) method once before the first check, for example in the constructor of the window.

The same event works for other controls, with two differences. The dialog starts with the words of a `Check` call, and the handler hands it the text of the next control by passing that text to [SetNext](https://docs.textcontrol.com/tx-spell-dotnet-wpf/reference/txtextcontrol-wpf-proofing/spellcheckdialogstatemanager/setnext-method/llms-full.txt) as a string. And because there is no `TextControl`, the handler tests [NextIncorrectWord](https://docs.textcontrol.com/tx-spell-dotnet-wpf/reference/txtextcontrol-wpf-proofing/spellcheckdialogstatemanager/nextincorrectword-property/llms-full.txt) instead of `NextMisspelledWord`:

```csharp
private TextBox[] textBoxes;
private TextBox curTextBox;

private void buttonSpelling_Click(object sender, RoutedEventArgs e)
{
    textBoxes = new[] { textBox1, textBox2, textBox3, textBox4 };
    curTextBox = textBoxes[0];

    txSpellChecker1.Check(curTextBox.Text);
    txSpellChecker1.SpellCheckDialog.Show(this);
}

private void txSpellChecker1_SpellCheckDialogClosing(object sender, TXTextControl.WPF.Proofing.SpellCheckDialogClosingEventArgs e)
{
    if (e.StateManager.NextIncorrectWord != null) return;

    int index = Array.IndexOf(textBoxes, curTextBox);
    if (index == textBoxes.Length - 1) return;

    curTextBox = textBoxes[index + 1];
    e.StateManager.SetNext(curTextBox.Text);
    e.Cancel = true;
}
```

The corrections still reach each control through the [IncorrectWordChanging](https://docs.textcontrol.com/tx-spell-dotnet-wpf/reference/txtextcontrol-wpf-proofing/txspellchecker/incorrectwordchanging-event/llms-full.txt) event, as [Checking on Demand](https://docs.textcontrol.com/tx-spell-dotnet-wpf/spell-checking/on-demand/#check-a-textbox-in-the-dialog) shows; there, `textBox1` becomes `curTextBox`. The *SpellCheckDialog on multiple controls* sample for the `RichTextBox` checks four of them in one dialog:

![One Spelling dialog over four RichTextBox controls, showing a misspelled word of the first control.](https://docs.textcontrol.com/assets/img/tx-spell-dotnet-wpf/richtextbox-multi-dialog.b8007266.webp)

### Next Steps

- [Checking Text Parts](https://docs.textcontrol.com/tx-spell-dotnet-wpf/spell-checking/text-parts/llms-full.txt): decide which headers, footers and text frames the dialog checks.
- [User Dictionaries](https://docs.textcontrol.com/tx-spell-dotnet-wpf/dictionaries/user/llms-full.txt): the words users add, shared by every connected control.
- [Events](https://docs.textcontrol.com/tx-spell-dotnet-wpf/spell-checking/events/llms-full.txt): the other events of the dialog, such as `SpellCheckDialogOpening`.
- [Spell Checking Examples](https://docs.textcontrol.com/tx-spell-dotnet-wpf/examples/spell-checking/llms-full.txt): the samples of the setup for several controls.

---

## Nearby

- **Previous:** [Ignore Settings](https://docs.textcontrol.com/tx-spell-dotnet-wpf/spell-checking/ignore-settings/llms-full.txt)
- **Next:** [Checking Text Parts](https://docs.textcontrol.com/tx-spell-dotnet-wpf/spell-checking/text-parts/llms-full.txt)
