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

Software/Hardware used:
ASKED: November 20, 2008  1:59 AM
UPDATED: November 19, 2011  12:53 PM

Answer Wiki:
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
Last Wiki Answer Submitted:  November 20, 2008  3:37 pm  by  carlosdl   63,535 pts.
All Answer Wiki Contributors:  carlosdl   63,535 pts. , Flame   14,895 pts.
To see all answers submitted to the Answer Wiki: View Answer History.


Discuss This Question:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _


 

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 pts.

 

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 pts.

 

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 pts.

 

it tried method2 and it works..thanks a lot

twlp123

 165 pts.

 

if (string.Compare(input.Substring(i,1),input.Substring(i,1).ToUpper()) == 0)
whats the meaning of this code

 10 pts.