TX Spell .NET for Windows Forms

Hyphenation With TX Text Control

Topic

In a TX Text Control editor, TX Spell .NET divides the last word of a full line and moves the rest to the next line, as the user types and in every language that has a hyphenation list. Turning this on takes two properties. The sections below show what the user sees, how the language of the text chooses the rules, and how your code can change where a word is divided.

What the User Sees

Once hyphenation is on, a word that doesn't fit at the end of a line is no longer moved to the next line as a whole. It is divided at a hyphenation point, and a hyphen marks the division:

TX Text Control Words with a document whose lines end in divided words such as Win-dows, prop-erties and documenta-, with the Enable Hyphenation button of the ribbon switched on.

The division follows the text as it changes. While the user types, the word at the end of the line is divided as soon as it reaches the margin:

Text being typed before the last word of a line in TX Text Control Words, which divides the word hyphenation as hyphen- when it reaches the right margin.

Both pictures show TX Text Control Words, the demo application of TX Text Control, in an earlier version of its ribbon.

How It Works

Two properties of the TextControl are needed:

When a word doesn't fit into a line, the spell checker calculates where to divide it, from the hyphenation list of the word's language. The TextControl then raises its HyphenateWord event with that position, divides the word there and puts a hyphen at the end of the line.

Turn On Hyphenation

In the designer, select the TextControl and set its properties in the Properties window:

  1. Set SpellChecker to txSpellChecker1, the TXSpellChecker on the form.
  2. Set IsHyphenationEnabled to True.

The Properties window of textControl1 with IsHyphenationEnabled set to True and the drop-down of IsSpellCheckingEnabled open with True and False.

The same in code, for example in the constructor of the form after InitializeComponent:

// Connect the spell checker and hyphenate words at the end of a line
textControl1.SpellChecker = txSpellChecker1;
textControl1.IsHyphenationEnabled = true;

Run the application and type a long text: the last word of each line is divided when it doesn't fit. Create Your First Windows Forms Application sets up such an application from the start.

Switch Hyphenation On and Off in the Ribbon

With the ribbon of TX Text Control, users switch hyphenation on and off themselves. The Enable Hyphenations button is on the Proofing tab, a RibbonProofingTab, in the Spelling & Hyphenation group:

The Proofing tab of the ribbon with the buttons Enable Spell Checking, Enable Hyphenations, Thesaurus, Spelling, Load Dictionary, Load Thesaurus File and Manage Spelling & Hyphenation.

Except for the language button, the buttons of the tab are enabled only when the TextControl has a spell checker connected through SpellChecker.

Which List Hyphenates Which Text

Every part of a document can have a language of its own, and each word is hyphenated with the hyphenation list of its language. A document with English and German paragraphs therefore needs hyph_en_US.dic and hyph_de_DE.dic in the HyphenationLists folder; Hyphenation Lists shows where to get them.

The user sets the language of the text in the Language dialog of TX Text Control, your code with the Culture of the selection or of a style, and language detection sets it for text without one; How Text Gets a Language describes the three ways.

The HyphenationLanguage property of the spell checker selects its default list. With its default value "(Auto)", it follows the language of the spell check dictionary. The settings of each list, such as the fewest characters on each line, are changed by the user on the Hyphenation tab of the Options Dialog, and in code as Hyphenation Lists shows.

Change Where a Word Is Divided

The HyphenateWord event lets your code look at each division and change it. Its HyphenateWordEventArgs hold:

PropertyContents
WordThe word that doesn't fit into the line
CultureThe language of the word
MaxDividePosThe largest position at which the word can be divided: with 3, at most the first three characters fit into the line
DividePosThe position the spell checker calculated. Set it to divide the word elsewhere

The TextControl divides the word at DividePos if it is not greater than MaxDividePos. This handler logs each division, which shows how the rules of a list work on your documents:

private void textControl1_HyphenateWord(object sender, TXTextControl.HyphenateWordEventArgs e)
{
    System.Diagnostics.Debug.WriteLine(
        $"{e.Word} ({e.Culture}): divided after {e.DividePos} of at most {e.MaxDividePos} characters");
}

Attach it in the constructor with textControl1.HyphenateWord += textControl1_HyphenateWord;, or in the Events view of the Properties window. To divide a word by other rules, assign another position to DividePos. GetHyphenationPoint of the spell checker returns the last hyphenation point before a position, with the list of any language. This handler divides every word by the British English rules of hyph_en_GB.dic, which must be in the HyphenationLists folder:

private void textControl1_HyphenateWord(object sender, TXTextControl.HyphenateWordEventArgs e)
{
    // Divide with the British English list, before the margin
    e.DividePos = txSpellChecker1.GetHyphenationPoint(e.Word, e.MaxDividePos, new System.Globalization.CultureInfo("en-GB"));
}

Hyphenating Text in Code explains GetHyphenationPoint and what it returns.

Next Steps