Articles in this section
Category / Section

How to show suggestions for spelling mistakes using context menu in UWP RichTextBox?

4 mins read

The UWP RichTextBox control provides API to check the spelling mistake of current word in selection and get the list of suggestions for the misspelled words (if any). Using this option, you can easily customize the spell-checking functionality in context menu.

 

The following sample code demonstrates how to initialize SfRichTextBoxAdv control and context menu for it.

XAML

<RichTextBoxAdv:SfRichTextBoxAdv x:Name="richTextBoxAdv" ManipulationMode="All" Grid.Row="1" xmlns:RichTextBoxAdv="using:Syncfusion.UI.Xaml.RichTextBoxAdv"/>

 

The following sample code demonstrates declaring a variable for holding the context menu instance for SfRichTextBoxAdv control.

C#

// Represents the context menu for SfRichTextBoxAdv
MenuFlyout flyout = null;

 

The following sample code demonstrates enabling the spell checker for SfRichTextBoxAdv control, initializing the context menu and adding event handlers for enabling context menu on right tap.

C#

// Enables the spell checker.
richTextBoxAdv.SpellChecker.IsEnabled = true; '
// Adds the language dictionary to spellchecker.
richTextBoxAdv.SpellChecker.Dictionaries.Add("ms-appx:///Assets/en_US.dic");
 
// Initializes the context menu
flyout = new MenuFlyout();
FlyoutBase.SetAttachedFlyout(richTextBoxAdv, flyout);
 
// Adds event handler for right tapped to enable context menu for spell-checking.
richTextBoxAdv.RightTapped += RichTextBoxAdv_RightTapped;
 
// Adds event handler for selection changed and flyout closed to enable radial menu after context menu closed.
richTextBoxAdv.SelectionChanged += RichTextBoxAdv_SelectionChanged;
flyout.Closed += Flyout_Closed;

 

The following sample code demonstrates the event handlers to enable context menu for spell-checking.

C#

// Called when right tapped in SfRichTextBoxAdv.
private void RichTextBoxAdv_RightTapped(object sender, RightTappedRoutedEventArgs e)
{
    CheckSpellingUsingContextMenu(e.GetPosition(richTextBoxAdv));
}
 
// Check spelling using context menu.
public void CheckSpellingUsingContextMenu(Point point)
{
    // Clears previous items in the context menu.
    flyout.Items.Clear();
    // Gets the cursor position.
    TextPosition start = richTextBoxAdv.Selection.Start;
    // Checks the spelling mistake of current word.
    SpellingError error = richTextBoxAdv.GetSpellingError(start);
    // If there is spelling mistake, shows context menu with suggestions(if any).
    if (error != null)
    {
        // Disables the built-in radial menu.
        richTextBoxAdv.EnableRadialMenu = false;
        // Gets the suggestions for spelling mistakes.
        List<string> suggestions = error.Suggestions.ToList<string>();
        if (suggestions.Count > 0)
        {
            for (int i = 0; i < suggestions.Count && i < 5; i++)
            {
                // Adds the suggested to the context menu.
                MenuFlyoutItem item = new MenuFlyoutItem();
                item.Text = suggestions[i];
                flyout.Items.Add(item);
                // Sets the change spelling command with suggested text as parameter.
                item.Command = richTextBoxAdv.ChangeSpellingCommand;
                item.CommandParameter = item.Text;
            }
        }
        else
            flyout.Items.Add(new MenuFlyoutItem() { Text = "No suggestions", IsEnabled = false });
        // Shows the context menu.
        flyout.ShowAt(richTextBoxAdv, point);
    }
    else
    {
        // Enables the built-in radial menu.
        richTextBoxAdv.EnableRadialMenu = true;
        richTextBoxAdv.Focus(FocusState.Pointer);
    }
}
 
// Called when selection is changed.
private void RichTextBoxAdv_SelectionChanged(object obj, Syncfusion.UI.Xaml.RichTextBoxAdv.SelectionChangedEventArgs args)
{
    // Enables the built-in radial menu.
    richTextBoxAdv.EnableRadialMenu = true;
}
 
 
// Called when the context menu is closed
private void Flyout_Closed(object sender, object e)
{
    // Enables the built-in radial menu on context menu closed.
    richTextBoxAdv.EnableRadialMenu = true;
    richTextBoxAdv.Focus(FocusState.Pointer);
}

 

The SfRichTextBoxAdv control provides API for retrieving the bounds of the cursor position. Using this option, you can easily customize enabling context menu through code behind.

 

The following sample code demonstrates how to enable context menu through code behind.

 

C#

// Gets the selection start text position.
TextPosition start = richTextBoxAdv.Selection.Start;
// Gets the bounds of the text position.
Rect rect = start.GetRect();
// Shows context menu at the cursor position.
CheckSpellingUsingContextMenu(new Point(rect.X, rect.Y));

 

You can find the sample from following link.

Sample

Conclusion

I hope you enjoyed learning about how to show suggestions for spelling mistakes in UWP RichTextBox.

You can refer to our UWP RichTextEditor feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our UWP RichTextEditor example to understand how to create and manipulate data.


For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied