Make the first letter after Single quote (Apostrophe) capital in C# .Net

Last Reply on Feb 28, 2015 01:46 AM By Mudassar

Posted on Feb 28, 2015 01:46 AM

Hi,

I want to make first letter capital after the apostrophe "'"

for e.g D'Souza instead of D'souza.

Please  provide me solution.

 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;

            CultureInfo cI = Thread.CurrentThread.CurrentCulture;
            TextInfo textInfo = cI.TextInfo;
            txtName.Text = textInfo.ToTitleCase(txtName.Text);

            txtName.SelectionStart = i;
        }

 

Posted on Feb 28, 2015 01:46 AM Modified on on Mar 02, 2015 01:12 AM

I have created a simple example, now you can take it further

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();
            }
        }
        txtName.SelectionStart = i;
}