AdibRahman77
asked on
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(objec t sender, KeyPressEventArgs e)
{
if (!char.IsDigit(e.KeyChar) && !char.IsControl(e.KeyChar) )
{
//do nuthing
}
else
{
e.Handled = true;
}
}
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(objec
{
if (!char.IsDigit(e.KeyChar) && !char.IsControl(e.KeyChar)
{
//do nuthing
}
else
{
e.Handled = true;
}
}
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
try with:
if (!char.IsDigit(e.KeyChar) && !char.IsPunctuation(e.KeyC har))
{
//do nuthing
}
else
{
e.KeyChar = char.ToUpper(e.KeyChar);
e.Handled = true;
}
if (!char.IsDigit(e.KeyChar) && !char.IsPunctuation(e.KeyC
{
//do nuthing
}
else
{
e.KeyChar = char.ToUpper(e.KeyChar);
e.Handled = true;
}
BTW: if you include \d in you regex it will also filter out numbers: -
string sSkip = @"[^\d\.,:;]";
>> char.IsPunctuation(e.KeyCh ar)
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.
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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
In fact you can check for valid characters instead of invalid, in this case I think would be simpler.
>> I think regex is too much complex
I guess that all depends on whether you think a regex is complex or not :)
I guess that all depends on whether you think a regex is complex or not :)
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.
>> 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).
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).
>>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 ;)
It is valid for me to say that it is complex ;)
>> 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 ;)
Yup, but it's also valid for me to point out that's a subjective opinion ;)
More info on regexes...
http://www.regular-expressions.info/