# Loading and Managing Dictionaries

> Load and manage dictionaries in Windows Forms: DictionariesDirectoryPath, Language (Auto), AvailableDictionaries, Dictionaries.Add and Remove, resources.

- **Product:** TX Spell .NET for Windows Forms
- **Section:** Documentation
- **Kind:** Topic
- **Path:** TX Spell .NET for Windows Forms > Dictionaries and Languages > Loading and Managing Dictionaries
- **3373 words**
- **Web URL:** https://docs.textcontrol.com/tx-spell-dotnet-winforms/dictionaries/managing/
- **LLMs URL:** https://docs.textcontrol.com/tx-spell-dotnet-winforms/dictionaries/managing/llms.txt
- **LLMs-Full URL:** https://docs.textcontrol.com/tx-spell-dotnet-winforms/dictionaries/managing/llms-full.txt

---

Every check of TX Spell .NET runs against the dictionaries in one collection. Which dictionaries it holds decides what is correct: English only by default, English and German once you add the German dictionary, or a specialist vocabulary on top. Here is where the spell checker finds dictionary files, which one it loads by itself, and how your code adds, switches off and removes dictionaries while the application runs.

### Where the Spell Checker Finds Dictionaries

TX Spell .NET looks for dictionary files in its *default dictionary folder*, the folder *Dictionaries* next to the application or, on the development machine, below the *Assembly* folder of the installation. Both places are what the default of [DictionariesDirectoryPath](https://docs.textcontrol.com/tx-spell-dotnet-winforms/reference/txtextcontrol-proofing/txspellchecker/dictionariesdirectorypath-property/llms-full.txt), "(Auto)", stands for; [Installing Dictionaries and Language Data](https://docs.textcontrol.com/tx-spell-dotnet-winforms/installation/language-data/#where-the-spell-checker-looks) shows how to use another folder.

### Which Dictionary Is Loaded First

The [Language](https://docs.textcontrol.com/tx-spell-dotnet-winforms/reference/txtextcontrol-proofing/txspellchecker/language-property/llms-full.txt) property of the spell checker chooses the dictionary it loads by itself, the *default dictionary*. Its value is "(Auto)" when you drop a `TXSpellChecker` on a form:

1. The spell checker loads the dictionary of the language the application runs in, if the default folder has it.
2. Otherwise it loads a dictionary of a close language: in a British English environment with only *en\_US.dic* and *de\_DE\_frami.dic* in the folder, it takes *en\_US.dic*.
3. Otherwise it loads *en\_US.dic*, and if that is missing too, the first dictionary in the folder.

To fix the default dictionary instead, set `Language` to the file name of a dictionary in the default folder, in the **Properties** window or in code:

```csharp
// Use the German dictionary as the default dictionary
txSpellChecker1.Language = "de_DE_frami.dic";
```

A value ending in *.txd* loads a user dictionary instead, and "(none)" loads no dictionary at all, for an application that adds only dictionaries of its own. The chosen dictionary has its [IsSelectedAsDefault](https://docs.textcontrol.com/tx-spell-dotnet-winforms/reference/txtextcontrol-proofing/dictionary/isselectedasdefault-property/llms-full.txt) property set to `true`.

### See Which Dictionaries Are There

Two properties answer the two questions a program asks:

- **[AvailableDictionaries](https://docs.textcontrol.com/tx-spell-dotnet-winforms/reference/txtextcontrol-proofing/txspellchecker/availabledictionaries-property/llms-full.txt)** is an array of the file names in the default folder, whether they are loaded or not, such as `de_DE_frami.dic` and `en_US.dic`. It is the list to offer in a language menu of your application.
- **[Dictionaries](https://docs.textcontrol.com/tx-spell-dotnet-winforms/reference/txtextcontrol-proofing/txspellchecker/dictionaries-property/llms-full.txt)** is the [DictionaryCollection](https://docs.textcontrol.com/tx-spell-dotnet-winforms/reference/txtextcontrol-proofing/dictionarycollection/llms-full.txt) of the dictionaries that are loaded and used.

Each entry of `Dictionaries` is a [Dictionary](https://docs.textcontrol.com/tx-spell-dotnet-winforms/reference/txtextcontrol-proofing/dictionary/llms-full.txt): an [OpenOfficeDictionary](https://docs.textcontrol.com/tx-spell-dotnet-winforms/reference/txtextcontrol-proofing/openofficedictionary/llms-full.txt) for a Hunspell dictionary, or a [UserDictionary](https://docs.textcontrol.com/tx-spell-dotnet-winforms/reference/txtextcontrol-proofing/userdictionary/llms-full.txt) for a word list of your own. These properties describe it:

| Property | Holds |
|---|---|
| [Name](https://docs.textcontrol.com/tx-spell-dotnet-winforms/reference/txtextcontrol-proofing/dictionary/name-property/llms-full.txt) | The file name, such as `en_US.dic` |
| [DisplayName](https://docs.textcontrol.com/tx-spell-dotnet-winforms/reference/txtextcontrol-proofing/dictionary/displayname-property/llms-full.txt) | The name the dialogs show; by default the file name |
| [Language](https://docs.textcontrol.com/tx-spell-dotnet-winforms/reference/txtextcontrol-proofing/dictionary/language-property/llms-full.txt) | The language, taken from the file name, such as German (Germany) for *de\_DE\_frami.dic*; `null` when the name carries none |
| [IsSpellCheckingEnabled](https://docs.textcontrol.com/tx-spell-dotnet-winforms/reference/txtextcontrol-proofing/dictionary/isspellcheckingenabled-property/llms-full.txt) | Whether words are checked against it; `true` by default |
| [IsGetSuggestionsEnabled](https://docs.textcontrol.com/tx-spell-dotnet-winforms/reference/txtextcontrol-proofing/dictionary/isgetsuggestionsenabled-property/llms-full.txt) | Whether it provides suggestions; `true` by default |
| [DictionaryEncoding](https://docs.textcontrol.com/tx-spell-dotnet-winforms/reference/txtextcontrol-proofing/dictionary/dictionaryencoding-property/llms-full.txt) | The encoding of the dictionary, a `System.Text.Encoding` |

### Add a Dictionary

Copy the files of the dictionary into the default folder, as [Hunspell and OpenOffice Dictionaries](https://docs.textcontrol.com/tx-spell-dotnet-winforms/dictionaries/hunspell/#install-a-dictionary) describes. Then create an `OpenOfficeDictionary` with the file name and add it with [Add](https://docs.textcontrol.com/tx-spell-dotnet-winforms/reference/txtextcontrol-proofing/dictionarycollection/add-method/llms-full.txt), for example when the form loads:

```csharp
using System;
using System.Windows.Forms;
using TXTextControl.Proofing;

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        Load += Form1_Load;
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        // Check German text as well as English text
        OpenOfficeDictionary german = new OpenOfficeDictionary("de_DE_frami.dic");
        txSpellChecker1.Dictionaries.Add(german);
    }
}
```

The code works as follows:

- The [constructor](https://docs.textcontrol.com/tx-spell-dotnet-winforms/reference/txtextcontrol-proofing/openofficedictionary/constructor/llms-full.txt) takes the name of the *.dic* or *.aff* file. A name without a path refers to the default folder. The file is read only when the dictionary is added to the collection.
- `Add` loads the dictionary, checks that it is valid, and returns its index in the collection.
- The new dictionary gets its language from its file name, here German (Germany).

From now on, a word is correct if one of the enabled dictionaries knows it. In a test with the sentence *This sentense has a mistake. Dieser Satz hat einen Fehlr.*, English alone reports *sentense*, *Dieser*, *Satz*, *einen* and *Fehlr*; with the German dictionary added, only *sentense* and *Fehlr* remain.

To check a text in one language only, pass the language to [Check](https://docs.textcontrol.com/tx-spell-dotnet-winforms/reference/txtextcontrol-proofing/txspellchecker/check-method/llms-full.txt), as [Check a String in One Language](https://docs.textcontrol.com/tx-spell-dotnet-winforms/dictionaries/multilingual/#check-a-string-in-one-language) shows.

### Check While a Large Dictionary Loads

By default, `Add` loads a dictionary completely before the spell checker uses it. [LoadDictionaryDelayed](https://docs.textcontrol.com/tx-spell-dotnet-winforms/reference/txtextcontrol-proofing/dictionarycollection/loaddictionarydelayed-property/llms-full.txt) of the `DictionaryCollection` changes this: set to `true`, it loads dictionaries partially when they are added, and the spell checker checks text while the rest is still loading:

```csharp
// Check text while the German dictionary is still loading
txSpellChecker1.Dictionaries.LoadDictionaryDelayed = true;
txSpellChecker1.Dictionaries.Add(new OpenOfficeDictionary("de_DE_frami.dic"));
```

The dictionary is loaded when it is added, so set the property before `Add`. The price is speed: until the dictionary is loaded completely, checking is slower than with a fully loaded one. For the full checking speed, leave the property at its default, `false`.

### Load a Dictionary From Memory

A dictionary doesn't have to be a file in a folder. A second constructor takes the lines of the *.dic* and the *.aff* file as string arrays, together with the language, so the dictionary can come from a database or from resources embedded in your assembly:

```csharp
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Reflection;
using System.Text;
using TXTextControl.Proofing;

private static string[] ReadLines(string resourceName, Encoding encoding)
{
    List<string> lines = new List<string>();

    using (Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName))
    using (StreamReader reader = new StreamReader(stream, encoding))
    {
        string line;
        while ((line = reader.ReadLine()) != null)
        {
            lines.Add(line);
        }
    }

    return lines.ToArray();
}

private void AddEmbeddedDictionary()
{
    // The German files are encoded in ISO-8859-1, as the SET line of the .aff file says
    Encoding encoding = Encoding.GetEncoding("ISO-8859-1");

    // The resource names depend on the default namespace and the folder of the files
    OpenOfficeDictionary german = new OpenOfficeDictionary(
        ReadLines("MyApp.Dictionaries.de_DE_frami.dic", encoding),
        ReadLines("MyApp.Dictionaries.de_DE_frami.aff", encoding),
        new CultureInfo("de-DE"));

    txSpellChecker1.Dictionaries.Add(german);
}
```

Add the two files to the project with the build action **Embedded Resource**. Read them with the encoding that the `SET` line of the *.aff* file names: `SET ISO8859-1` for the German dictionary, `SET UTF-8` for the English one. The blog article [TX Spell .NET 3.0: Loading Dictionaries from Embedded Resources](https://www.textcontrol.com/blog/2012/09/19/tx-spell-net-30-loading-dictionaries-from-embedded-resources/) describes the approach.

### Switch Off or Remove a Dictionary

A dictionary can stay loaded but play a smaller part:

- Set `IsSpellCheckingEnabled` to `false`, and the dictionary no longer decides whether a word is correct, but can still provide suggestions.
- Set `IsGetSuggestionsEnabled` to `false`, and it checks words but provides no suggestions.

To take a dictionary out entirely, find it with [GetItem](https://docs.textcontrol.com/tx-spell-dotnet-winforms/reference/txtextcontrol-proofing/dictionarycollection/getitem-method/llms-full.txt) and pass it to [Remove](https://docs.textcontrol.com/tx-spell-dotnet-winforms/reference/txtextcontrol-proofing/dictionarycollection/remove-method/llms-full.txt):

```csharp
Dictionary german = txSpellChecker1.Dictionaries.GetItem("de_DE_frami.dic");

if (german != null)
{
    txSpellChecker1.Dictionaries.Remove(german);
}
```

`GetItem` takes the full name, with the extension, and returns `null` when there is no such dictionary. `Add` and `Remove` throw an exception while the spell check dialog is open. Only a user dictionary can be added or removed then, and only while the options dialog that the **Options...** button of the spell check dialog opened is shown.

### React When Dictionaries Change

The spell checker raises [DictionaryAdded](https://docs.textcontrol.com/tx-spell-dotnet-winforms/reference/txtextcontrol-proofing/txspellchecker/dictionaryadded-event/llms-full.txt) and [DictionaryRemoved](https://docs.textcontrol.com/tx-spell-dotnet-winforms/reference/txtextcontrol-proofing/txspellchecker/dictionaryremoved-event/llms-full.txt) whenever the collection changes, whether your code or the user made the change. The [Dictionary](https://docs.textcontrol.com/tx-spell-dotnet-winforms/reference/txtextcontrol-proofing/dictionaryaddedeventargs/dictionary-property/llms-full.txt) property of the arguments is the dictionary concerned, for example to update a language menu of your application. [Events](https://docs.textcontrol.com/tx-spell-dotnet-winforms/spell-checking/events/llms-full.txt) lists all events.

### What Users Can Do

Users see the dictionaries in the options dialog, whose tab **Spell Checking Dictionaries** switches each one off for spell checking or for suggestions, and in the **Suggestions Dictionary** list of the spell check dialog, which chooses the dictionary the suggestions come from. With the ribbon of TX Text Control, the **Settings** group of the **Proofing** tab also has a **Load Dictionary** button. [The Spell Checking Dictionaries Tab](https://docs.textcontrol.com/tx-spell-dotnet-winforms/user-interface/options-dialog/#the-spell-checking-dictionaries-tab) and [The Suggestions Dictionary List](https://docs.textcontrol.com/tx-spell-dotnet-winforms/user-interface/suggestions/#the-suggestions-dictionary-list) describe both lists, and [Hide or Rename a Dictionary](https://docs.textcontrol.com/tx-spell-dotnet-winforms/user-interface/options-dialog/#hide-or-rename-a-dictionary) how to hide a dictionary from the user or give it a display name.

### Next Steps

- [User Dictionaries](https://docs.textcontrol.com/tx-spell-dotnet-winforms/dictionaries/user/llms-full.txt): let users add words, and keep them.
- [Multilingual Documents and Language Scopes](https://docs.textcontrol.com/tx-spell-dotnet-winforms/dictionaries/multilingual/llms-full.txt): check each language of a document with its dictionary.
- [Language Detection](https://docs.textcontrol.com/tx-spell-dotnet-winforms/dictionaries/language-detection/llms-full.txt): find out which languages a text contains, and load their dictionaries.

---

## Nearby

- **Previous:** [Hunspell and OpenOffice Dictionaries](https://docs.textcontrol.com/tx-spell-dotnet-winforms/dictionaries/hunspell/llms-full.txt)
- **Next:** [User Dictionaries](https://docs.textcontrol.com/tx-spell-dotnet-winforms/dictionaries/user/llms-full.txt)
