Click here to Skip to main content
15,881,281 members
Please Sign up or sign in to vote.
3.50/5 (2 votes)
See more:
Hello everybody,

Please if you know a special textbox component that only write alpha letters
in Uppercase.

I mean that when i write in textbox, allways the component show me uppercase letters

for example:
------------------------------------------

I writetextbox show
mamaMAMA
ProgrammingPROGRAMMING

-------------------------------------------

Please give me a resources where I can find about this special component.

Thanks in advance,
Posted
Updated 22-Mar-11 13:29pm
v2
Comments
#realJSOP 22-Mar-11 12:28pm    
What?
leocode7 22-Mar-11 14:01pm    
what what?? jajaja
pankajupadhyay29 22-Mar-11 12:32pm    
do you want to show text in UpperCase?? Is this window form application or web application.
leocode7 22-Mar-11 14:02pm    
Hello,
it's for form aplication and i want upper case, but I have good answers now.
Thanks anyway.

You don't need a special text box. You use a regular one, only need to filter our unwanted characters.

Use something like this:

C#
MyTextBox.KeyPress += delegate(object sender, KeyPressEventArgs eventArgs) {
    ee.Handled = true;
    char ch = Convert.ToChar(eventArgs.KeyChar);
    if (!char.IsLetter(ch))
        ee.Handled = true;
};


This is the equivalent lambda form of the same thing. It is more convenient but require C#.v.3 or later:
C#
MyTextBox.KeyPress += (sender, eventArgs) => {
    eventArgs.Handled = true;
    char ch = Convert.ToChar(eventArgs.KeyChar);
    if (!char.IsLetter(ch))
        eventArgs.Handled = true;
};


Call this code anywhere before you show the form. I usually call it from the form's constructor, at the very end of it.

—SA
 
Share this answer
 
v3
Comments
Sergey Alexandrovich Kryukov 22-Mar-11 13:23pm    
Sorry, I don't know what MAYUS means, but you also can filter out characters by their code points, etc. You can even white some predicate method like "bool IsGoodChar(char)".
--SA
Sergey Alexandrovich Kryukov 22-Mar-11 18:15pm    
Anyway, thanks for accepting my Answer.
Good luck, call again.
--SA
for window application use txt.CharacterCasing = CharacterCasing.Upper and for web application use following javascript
Java
function MakeUpper(event) {
           var charCode =  event.keyCode;
           if (charCode >= 97 && charCode <= 122) {
               event.keyCode = charCode - 32;
           }
           return true;
       }


and on pageload write --
C#
TextBox1.Attributes.Add("onkeypress", "MakeUpper(event)");


[Edit]
I checked this is working fine in IE but for others we need to make a small change
Java
<pre lang="cs">function MakeUpper(evt,obj) {
           var charCode = evt.which ? evt.which : evt.keyCode;
           if (charCode >= 97 && charCode <= 122) {
               obj.value += String.fromCharCode(charCode - 32);
               evt.returnValue = false;
               return false;
           }
           else {
               return true;
           }
       }



and in page load--
C#
TextBox1.Attributes.Add("onkeypress", "MakeUpper(event, this)");


[/Edit]
--Pankaj
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900