Click here to Skip to main content
15,895,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
The Below Is My Code For Capitalizing The First Letter In Each Word Of A String Insert/Typed In The TextBox ..The Code Is Well And Good...
But When I Tried To Type A Letter/Word In The Between The String,Then The Cursor Is Jumping To The End Of The String..I Had Tried A Lot..Pls Can Any One Help Me In This...
This Code Is In C#,Winforms:
C#
private void txtCName_KeyPress(object sender, KeyPressEventArgs e)
       {
           try
          {
          bool hasSingleWhitespace = txtCName.Text == " ";
              if (!(Char.IsLetter(e.KeyChar) || Char.IsControl(e.KeyChar) || Char.IsWhiteSpace(e.KeyChar)))
               e.Handled = true;
          char[] c = txtCName.Text.ToCharArray();
            int j;
           for (j = 0; j < txtCName.Text.Length; j++)
           {
               if (j == 0) c[j] = c[j].ToString().ToUpper()[0];
                 else c[j] = c[j].ToString().ToLower()[0];
           }
             txtCName.Text = new string(c);
            txtCName.Select(txtCName.Text.Length, 1);
          }
         catch (Exception ex)
        {
              MessageBox.Show(ex.Message);

         }
    }
Posted
Updated 6-Dec-12 0:12am
v2

Rather than executing your code in the KeyPress event, better do it in the Validating event. It will be executed when you validate your textbox (tab or enter) rather than each time you press a key on this control.

Please vote if this helped.
 
Share this answer
 
Comments
CHAITANYA KIRAN KASANI 6-Dec-12 12:31pm    
Actually I Need The Code In KeyPress Or TextChange
this can be done with few lines of code only.
Add
using System.Text.RegularExpressions;

Write This Code

var phrase = "Test WORD";
var rx = new System.Text.RegularExpressions.Regex(@"(?<=\w)\w");
var ConvertedValue = rx.Replace(phrase, new MatchEvaluator(m => m.Value.ToLowerInvariant()));
 
Share this answer
 
Comments
CHAITANYA KIRAN KASANI 6-Dec-12 7:20am    
Its Not Working Vijay...AnyWay ThanQ For Your Reply
try this one..


C#
CultureInfo cultureInfo   = Thread.CurrentThread.CurrentCulture;
TextInfo textInfo = cultureInfo.TextInfo;

Console.WriteLine(textInfo.ToTitleCase(title));


please, do mark as answer if it helps.

happy to help :)
 
Share this answer
 
Comments
CHAITANYA KIRAN KASANI 7-Dec-12 3:16am    
It Works Only On Text Leave Event, But I Need It On KeyPress Or TextChanged Events
AnkitGoel.com 7-Dec-12 3:38am    
Please implement KeyPress or TextChanged Event and use the same code in that event-

CultureInfo cultureInfo = Thread.CurrentThread.CurrentCulture;
TextInfo textInfo = cultureInfo.TextInfo;

Console.WriteLine(textInfo.ToTitleCase(title));

The problem your are facing is due to your coding style not because of this code. Please recheck and mark as answer if worked. May be u can send your code here if u need to be reviewed.
Try this
C#
string phrase = txtCName.Text;
phrase = char.ToUpper(phrase[0]) + phrase.Substring(1).ToLower();
txtCName.Text = phrase; 

--SJ
 
Share this answer
 
Comments
CHAITANYA KIRAN KASANI 6-Dec-12 12:34pm    
Cannot rectify the bug
codeninja-C# 6-Dec-12 23:48pm    
The above code definitely capitalize the first character of a word.
can you elaborate your problem?
CHAITANYA KIRAN KASANI 7-Dec-12 3:13am    
It Is Only Capitalizing A First Letter In Whole String And It Works Only In Text Leave Event..But I Need It In TextChanged..Anyway Good Try..ThanQ
Chaitanya its working in mine case
Might be you are missing something.

Also you can use textbox leave event insteed of key press and then split the text based on space and process words as usual.

C#
//Method One
               var phrase = "Test WORD";
               Trace.Write(phrase);
               var rx = new System.Text.RegularExpressions.Regex(@"(?<=\w)\w");
               var newString = rx.Replace(phrase, new MatchEvaluator(m => m.Value.ToLowerInvariant()));
               //Method One
               string s = "Test WORD";
               StringBuilder sb = new StringBuilder(s.Length);
               bool capitalize = true;
               foreach (char c in s)
               {
                   sb.Append(capitalize ? Char.ToUpper(c) : Char.ToLower(c));
                   capitalize = !Char.IsLetter(c);
               }
               s = sb.ToString();
 
Share this answer
 
v2
Comments
CHAITANYA KIRAN KASANI 7-Dec-12 3:01am    
The First Method Had A Bug That Only For When I Type Capital Letters It Is Working But When I Type Small Letters Its Not Working..And Second Method Is Good , Its Only Working On Text Leave Event But I Need It In TextChanged or KeyPress Events...Any Way Good Effort.... ThanQ Vijay
Singh Vijay Kumar 7-Dec-12 3:57am    
Remove Trace.Write(phrase); since its my application specific i forgot to remove while pasting.

Try this one
string sFirstChar = textBox1.Text.Substring(0, 1).ToUpper();
string sSecondChar = textBox1.Text.ToLower();
string sFinal = sFirstChar;
sFinal += sSecondChar.Remove(0,1);
textBox1.Text = sFinal;
textBox1.SelectionStart = textBox1.Text.Length;
CHAITANYA KIRAN KASANI 7-Dec-12 4:18am    
sorry...it has same bug as my code got...
but i had rectified my bug with a simple code..

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