Convert first letter to uppercase

165 pts.
Tags:
C#
How do I program (c#) a textbox input to automatically convert the first character to uppercase as I type the words?

Answer Wiki

Thanks. We'll let you know when a new response is added.

I don’t know of a way to capitalize the input as the user types.

The CharacterCasing property is used to perform case conversions, but it allows upper case or lower case (or normal) for the complete text.

One workaround could be to write a function to capitalize the text before you process it in your application, or when some event occur in the text box (TextChanged, or Validated for example).

The capitalize function could look like this:

<pre>public static string Capitalize(string input)
{
if(string.IsNullOrEmpty(input)) return string.Empty;
return input.Substring(0, 1).ToUpper() + input.Substring(1).ToLower();
}</pre>

———————————————————–

This piece of code <a href=”http://dqsd.svn.sourceforge.net/viewvc/dqsd/trunk/dqsd/searches/ccase.xml”>here</a> might be a good place to start as far a word capping function.
Good Luck!
-Flame

Discuss This Question: 5  Replies

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
  • Twlp123
    thanks but your answer does not resoleve my question. Delphi function does it perfectly for starting words in a sentence, and the user just keep typing without having to resort to Shift key combination. The user should see the capitalised starting letter as they continue to type
    165 pointsBadges:
    report
  • BobBeechey
    Here is one w
            private void textBox1_TextChanged(object sender, EventArgs e)
            {
                if ((textBox1.Text.Length) == 1)
                {
                    textBox1.Text = textBox1.Text[0].ToString().ToUpper();
                    textBox1.Select(2, 1);
                   
                }
            }
    
    750 pointsBadges:
    report
  • BobBeechey
    Let us expand my earlier ideas, so that the first letter will always be capitalised and the rest be forced to lowercase. Method 1 - use VB.NET which offers
    MyNewString = StrConv(MyString, vbProperCase)
    
    Method 2 - convert to character array to freely manipulate each letter. This has the advantage of over-riding the caps lock but only capitalises the first letter of the first word. Method 3 - use a TextInfo object which has a ToTitleCase method. This has thye advantage of capitalising thye first letter of each separate word but is over-ridden by caps lock. (This method requires using System.Globalization and System.Threading. Here is the code for methods 2 and 3
    private void textBox1_TextChanged(object sender, EventArgs e)
            {
                char[] c = textBox1.Text.ToCharArray();
                int j;
                for (j = 0;j<textBox1.Text.Length;j++) {
                    if (j==0) c[j]=c[j].ToString().ToUpper()[0];
                    else c[j] = c[j].ToString().ToLower()[0];
                }
                textBox1.Text = new string(c);
                textBox1.Select(textBox1.Text.Length,1);
                }
    
            private void textBox2_TextChanged(object sender, EventArgs e)
            {
                CultureInfo cI = Thread.CurrentThread.CurrentCulture;
                TextInfo textInfo = cI.TextInfo;
                textBox2.Text = textInfo.ToTitleCase(textBox2.Text);
                textBox2.Select(textBox2.Text.Length, 1);
            }
    750 pointsBadges:
    report
  • Twlp123
    it tried method2 and it works..thanks a lot twlp123
    165 pointsBadges:
    report
  • 12345678905850053
    if (string.Compare(input.Substring(i,1),input.Substring(i,1).ToUpper()) == 0) whats the meaning of this code
    10 pointsBadges:
    report

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Thanks! We'll email you when relevant content is added and updated.

Following