WPF SpellChecker in VB.NET

In this article you will learn about Spellcheck an inbuilt feature of WPF.
  • 1703

WPF has an inbuilt feature of Spellcheck. SpellCheck is added to TextBoxBase object, which inherits all the textual input controls automatically. In this article I would like to introduce how you can explore this feature to your own application.

Code for SpellCheck

<Window x:Class="SpellCheckerSample.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1">
    <Window.Resources>
        <Style TargetType="{x:Type TextBoxBase}" x:Key="txtBasic">
            <Setter Property="SpellCheck.IsEnabled" Value="true" />
            <Setter Property="SpellCheck.SpellingReform" Value="PreAndPostreform" />
        </Style>
        <Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource txtBasic}" />
        <Style TargetType="{x:Type RichTextBox}" BasedOn="{StaticResource txtBasic}" />
        <ContextMenu x:Key="ctxMenu" >
            <MenuItem Command="ApplicationCommands.Cut"></MenuItem>
            <MenuItem Command="ApplicationCommands.Copy"></MenuItem>
            <MenuItem Command="ApplicationCommands.Paste"></MenuItem>
        </ContextMenu>
    </Window.Resources>
    <Grid> 
       
<StackPanel Height="150" Margin="50">
            <TextBlock Text="Enter Text" FontSize="12">
            </TextBlock>
            <TextBox x:Name="txtBox" AcceptsReturn="True" Keyboard.KeyUp="txtBox_KeyUp" ContextMenu="{StaticResource ctxMenu}" ContextMenuOpening="txtBox_ContextMenuOpening" FontSize="13">
            </TextBox>       
       
</StackPanel>
    </Grid>
</
Window>

In the above example, you can see the textbox have SpellChecker enabled. This ensures the textbox automatically detect misspelled words and will put an underline on the word. When you run the application, you will see the textbox to appear like this :

wpfspellcheker1.gif
You can see all the misspelled word is shown with red underline. These words can easily be corrected by using the TextBox default ContextMenu. Right clicking on the word will give you a list of suggestions for the current word which you might choose to change the word in the paragraph.

Conclusion

Here we discussed about WPF SpellCheck option which is use to build a better solution for the client. I hope this article will help you to a great extent.

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.