Solved

Winform filter text in a textbox

Posted on 2007-12-03
12
703 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
Business Impact of IT Communications

What are the business impacts of how well businesses communicate during an IT incident? Targeting, speed, and transparency all matter. Find out more in this infographic.

 
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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

726 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