Click here to Skip to main content
15,886,078 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to capitalize every first letter in a textBox text and I want when the user start entering the text it will automatically capitalize first letter...
I tried this -:
C#
private void textBox1_TextChanged(object sender, EventArgs e)
        {
            CultureInfo cultureInfo = System.Threading.Thread.CurrentThread.CurrentCulture;
            TextInfo textInfo = cultureInfo.TextInfo;
            textBox1.Text= textInfo.ToTitleCase(textBox1.Text);
        }

but when the first letter capitalized it set the cursor position at the beginning of the text so the text is reversed..
If I want to enter "jayanta" it shows "Atnayaj" But correct is "Jayanta"..

I want it like, when we set CharacterCasing property to UPPER then it change the text lower to upper case without changing the cursor position in the textBox...

How to fix it??
thanks in advance....
Posted
Updated 8-Mar-13 7:17am
v2

1 solution

use this code into textChanged ! hope help you !
C#
char[] v=textBox1.Text.ToCharArray();
string s = v[0].ToString().ToUpper();
for (int b = 1; b < v.Length; b++)
     s += v[b].ToString().ToLower();
textBox1.Text = s;
textBox1.Select(textBox1.Text.Length,0);
 
Share this answer
 
v2
Comments
JayantaChatterjee 8-Mar-13 13:13pm    
Sir, Same result, Its set cursor position at the beginning of the text..
I want it like, when we set CharacterCasing property to UPPER then it change the text lower to upper case without changing the cursor position in textBox...
Member 848045 19-Dec-13 19:41pm    
Thank you for the solution it helped me
ali_heidari_ 8-Mar-13 13:20pm    
see solution again, i improved it! (last line)
JayantaChatterjee 8-Mar-13 13:24pm    
Thanks a Lottttttttttt Sir....
It works..
ali_heidari_ 8-Mar-13 13:26pm    
actually your answer is last line!

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