2
Reply

Had A Bug In Captilizing The First Word In A String

Ask a question
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:
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);
 
}
}

Answers (2)