• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 726
  • Last Modified:

Winform filter text in a textbox

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
AdibRahman77
Asked:
AdibRahman77
  • 7
  • 5
2 Solutions
 
evilrixSenior Software Engineer (Avast)Commented:
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
 
evilrixSenior Software Engineer (Avast)Commented:
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
 
Jaime OlivaresSoftware ArchitectCommented:
try with:

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

0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
evilrixSenior Software Engineer (Avast)Commented:
BTW: if you include \d in you regex it will also filter out numbers: -
string sSkip = @"[^\d\.,:;]";

Open in new window

0
 
evilrixSenior Software Engineer (Avast)Commented:
>> 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
 
Jaime OlivaresSoftware ArchitectCommented:
I think regex is too much complex and unnecessary, string.IndexOf() will do the job faster.

if ( ".,:;".IndexOf(e.KeyChar) >= 0)
{
    // do anything
}
0
 
Jaime OlivaresSoftware ArchitectCommented:
In fact you can check for valid characters instead of invalid, in this case I think would be simpler.
0
 
evilrixSenior Software Engineer (Avast)Commented:
>> I think regex is too much complex
I guess that all depends on whether you think a regex is complex or not :)
0
 
Jaime OlivaresSoftware ArchitectCommented:
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
 
evilrixSenior Software Engineer (Avast)Commented:
>> 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
 
Jaime OlivaresSoftware ArchitectCommented:
>>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
 
evilrixSenior Software Engineer (Avast)Commented:
>> 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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 7
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now