Solved

Winform filter text in a textbox

Posted on 2007-12-03
12
696 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
>> 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
Comment Utility
I think regex is too much complex and unnecessary, string.IndexOf() will do the job faster.

if ( ".,:;".IndexOf(e.KeyChar) >= 0)
{
    // do anything
}
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 55

Expert Comment

by:Jaime Olivares
Comment Utility
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
Comment Utility
>> 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
Comment Utility
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
Comment Utility
>> 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
Comment Utility
>>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
Comment Utility
>> 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
visual Basic Net vs c# net 8 53
asp.net bundle 8 32
Please explain: Aspect Oriented Programming 2 51
dynamic menu in asp.net c# 11 25
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

772 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

10 Experts available now in Live!

Get 1:1 Help Now