Wednesday, 16 November 2011

WPF TextBox

How to enable spell checking

TextBox and RichTextBox provide an out-of-the-box spell checking functionality. It is available for the following languages: English, Spanish, German and French. It can be enabled by setting the attached property SpellCheck.IsEnabled to true.
 
    <TextBox SpellCheck.IsEnabled="True" Language="en-US" />
 
 

How to validate input

By using a regular expression, you can easily limit and validate the input of the user. The following code snippet shows how to do it:
 
protected override void OnTextInput(TextCompositionEventArgs e)
{
    string fullText = Text.Remove(SelectionStart, SelectionLength) + e.Text;
    if (_regex != null && !_regex.IsMatch(fullText))
    {
        e.Handled = true;
    }
    else
    {
        base.OnTextInput(e);
    }
}
 
 

No comments:

Post a Comment