Solved

Winform filter text in a textbox

Posted on 2007-12-03
12
702 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
How our DevOps Teams Maximize Uptime

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

 
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
 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
C# Get Current Exchange Rate from XE 9 42
Help with Report Viewer in VS2013 6 32
SSIS Column mapping 5 40
SQL State HYT00. Timeout expired proplem 8 42
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

732 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