Solved

Winform filter text in a textbox

Posted on 2007-12-03
12
697 Views
Last Modified: 2013-12-16
Hi all,

I want Skip . ,  : , ;  and Numbers when a key on a textbox. Also Is it poosible to change small letters to Caps as I key.I did the below to skip numbers but other characters I am not able to skip.

 private void txtInitials_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsDigit(e.KeyChar) && !char.IsControl(e.KeyChar))
            {
                               

              //do nuthing
            }
            else
            {
                e.Handled = true;
            }

        }


0
Comment
Question by:AdibRahman77
  • 7
  • 5
12 Comments
 
LVL 40

Assisted Solution

by:evilrix
evilrix earned 200 total points
ID: 20399070
Try something like this: -
            string sSkip = @"[^\.,:;]";

            Match m = Regex.Match(e.KeyChar.ToString(), sSkip);

            if (m.Success)

            {

                e.Handled = true;

            }

Open in new window

0
 
LVL 40

Expert Comment

by:evilrix
ID: 20399075
The regular expression will only be true (m.Success) if it cannot find any of the chars you specified. NB. it is necessary to escape the . as it is a special char in a regex.

More info on regexes...

http://www.regular-expressions.info/
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20399080
try with:

            if (!char.IsDigit(e.KeyChar) && !char.IsPunctuation(e.KeyChar))
            {
              //do nuthing
            }
            else
            {
                e.KeyChar = char.ToUpper(e.KeyChar);
                e.Handled = true;
            }

0
 
LVL 40

Expert Comment

by:evilrix
ID: 20399108
BTW: if you include \d in you regex it will also filter out numbers: -
string sSkip = @"[^\d\.,:;]";

Open in new window

0
 
LVL 40

Expert Comment

by:evilrix
ID: 20399124
>> char.IsPunctuation(e.KeyChar)

Won't that also include other punctuation chars that are not listed (which may or may not be desirable)?

http://msdn2.microsoft.com/en-us/library/6w3ahtyy.aspx

Valid punctuation marks are members of the following categories in UnicodeCategory: -

   ConnectorPunctuation
   DashPunctuation
   OpenPunctuation
   ClosePunctuation
   InititalQuotePunctuation
   FinalQuotePunctuation
   OtherPunctuation.
0
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 300 total points
ID: 20399134
I think regex is too much complex and unnecessary, string.IndexOf() will do the job faster.

if ( ".,:;".IndexOf(e.KeyChar) >= 0)
{
    // do anything
}
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20399149
In fact you can check for valid characters instead of invalid, in this case I think would be simpler.
0
 
LVL 40

Expert Comment

by:evilrix
ID: 20399151
>> I think regex is too much complex
I guess that all depends on whether you think a regex is complex or not :)
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20399431
it is complex for a beginner user that just want to detect ONE character in a list of characters, with no kind of complex needs like specific character/word sequencing. All this besides the fact that regex is usually has slower performance than other alternatives.

0
 
LVL 40

Expert Comment

by:evilrix
ID: 20399477
>> it is complex for a beginner user that just want to detect ONE character in a list of characters, with no kind of complex needs like specific character/word sequencing
So I shouldn't offer it as an option? It's a valid way to solve this problem, I think the OP has the right to choose if they wish to use it or not!

>> All this besides the fact that regex is usually has slower performance than other alternatives.
Most modern DFA and NFA regex engines have advanced optimization techniques; however, I agree that for very simple checks an alternative may prove more efficient (but less flexible).
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20399585
>>So I shouldn't offer it as an option? It's a valid way to solve this problem
It is valid for me to say that it is complex ;)
0
 
LVL 40

Expert Comment

by:evilrix
ID: 20399603
>> It is valid for me to say that it is complex ;)
Yup, but it's also valid for me to point out that's a subjective opinion ;)
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

28 Experts available now in Live!

Get 1:1 Help Now