Click here to Skip to main content
Click here to Skip to main content

Filtered Textbox

, 9 Oct 2005
Rate this:
Please Sign up or sign in to vote.
Demonstrates how to impliment a textbox who's input is regulated via. a regular expression.
<!-- Article Starts - DO NOT ADD HTML/BODY START TAGS-->

Introduction

Have you ever needed to create a textbox that only allows you to enter numbers? Or letters? Or strings that don't start with a number? C# 2.0 addresses this point to a certain extent with the masked textbox. However, this does not help if you need to use more complex rules to define the validation.

To this end, I created a FilteredTextBox, that uses a regular expression filter to define what can be entered in the textbox. If it cannot be entered, it will not appear (unlike a post-validation, that would create an error message, or cleanup and remove invalid characters after)

Using the Code

To use the compiled componant, enter a regular expression that for any acceptable string in the textbox will create a match in the Filter property.

Adding Sound

I excluded this to enable the code to work with .NET 1.1 However, if you are using 2.0, or can do something similar in 1.1, it may be a good idea to provide audio feedback when an invalid character is typed. I did this by adding a boolean property of ShouldPlaySound and modiftying the code to match below:
if (!regexFilter.IsMatch(inputText))
{
    if (PlayErrorSound)
        SystemSounds.Beep.Play();
    e.Handled = true;
}

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

[ICR]

United Kingdom United Kingdom
No Biography provided

Comments and Discussions

 
Generaloptimized for filtering every char Pinmemberjopomo20-Nov-06 5:35 
GeneralBug in filteredTextBox_KeyPress PinmemberPerry211-Apr-06 14:10 
GeneralWM_PASTE Pinsusstonyt9-Oct-05 21:27 
GeneralRe: WM_PASTE Pinmember[ICR]9-Oct-05 22:20 
GeneralUmm... PinmemberThe_Mega_ZZTer9-Oct-05 8:45 
GeneralRe: Umm... Pinmember[ICR]9-Oct-05 8:56 
GeneralRe: Umm... PinmemberThe_Mega_ZZTer9-Oct-05 8:59 
GeneralRe: Umm... Pinmember[ICR]9-Oct-05 9:06 
np. You do bring up a good possible problem. I shall look into how you would do that and post it, as it isn't a clear solution to anyone not that familiar (as indeed I myself am not) with regular expressions.
GeneralRe: Umm... Pinmember[ICR]9-Oct-05 12:48 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.141223.1 | Last Updated 9 Oct 2005
Article Copyright 2005 by [ICR]
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid