How to make first letter of each word and character after Apostrophe (Single quote) Capital in C# .Net

Last Reply on Mar 02, 2015 01:28 AM By Mudassar

Posted on Mar 02, 2015 01:13 AM

Hi, 

I want to make First Letter capital after Apostrophe Below is the code 

For e.g I want name should be written in this way  Roshan D'Souza

But below code is giving me output as Roshan d'Souza

Please Help me.

 private void txtName_TextChanged(object sender, EventArgs e)
        {                     
            int i = txtName.SelectionStart;
            if (txtName.Text.Length == 1)
            {
                txtName.Text = txtName.Text.ToUpper();
            }
            int index = txtName.Text.IndexOf("'");
            if (index != -1)
            {
                string charAfterApostrophe = txtName.Text.Split('\'')[1];
                if (charAfterApostrophe.Length == 1)
                {
                    txtName.Text = txtName.Text.Substring(0, txtName.Text.Length - 1) + charAfterApostrophe.ToUpper();
                }
            }
            txtName.SelectionStart = i;
        }

 

Posted on Mar 02, 2015 01:28 AM

This way

private void txtName_KeyPress(object sender, KeyPressEventArgs e)
{
    char appostrophe = '\'';
    char space = ' ';

    char backspace = (char)8;
    char delete = (char)127;

    e.Handled = !(char.IsLetter(e.KeyChar) ||
                    e.KeyChar == space ||
                    e.KeyChar == appostrophe ||
                    e.KeyChar == backspace ||
                    e.KeyChar == delete);
}

private void txtName_TextChanged(object sender, EventArgs e)
{
    int i = txtName.SelectionStart;
    if (txtName.Text.Length == 1)
    {
        txtName.Text = txtName.Text.ToUpper();
    }
    int index = txtName.Text.IndexOf("'");
    if (index != -1)
    {
        string charAfterApostrophe = txtName.Text.Split('\'')[1];
        if (charAfterApostrophe.Length == 1)
        {
            txtName.Text = txtName.Text.Substring(0, txtName.Text.Length - 1) + charAfterApostrophe.ToUpper();
        }
    }

    index = txtName.Text.IndexOf(" ");
    if (index != -1)
    {
        string charAfterSpace = txtName.Text.Split(' ')[1];
        if (charAfterSpace.Length == 1)
        {
            txtName.Text = txtName.Text.Substring(0, txtName.Text.Length - 1) + charAfterSpace.ToUpper();
        }
    }
    txtName.SelectionStart = i;
}