Solved

Convert first letter in a string to capital

Posted on 2014-10-21
5
118 Views
Last Modified: 2014-10-21
I have a Text Box, (say txtBox1) which I am asking the user to enter some text in.
I need to check if the first letter in the box's text is uppercase.
If not uppercase convert ONLY the first letter to uppercase and keep the remaining letters as lowercase.

Thank you
0
Comment
Question by:adamtrask
  • 3
  • 2
5 Comments
 
LVL 9

Expert Comment

by:dustock
ID: 40395461
There might be more efficient ways to do this, but this will give you the result your looking for.  In the Leave event for your text box drop in this line of code.

        private void textBox1_Leave(object sender, EventArgs e)
        {
            textBox1.Text = textBox1.Text[0].ToString().ToUpper() + textBox1.Text.Substring(1);
        }

Open in new window

0
 

Author Comment

by:adamtrask
ID: 40395595
Thank you. This partly works as it changes the first letter to uppercase, but if the user enters an uppercase letter in the middle of the word like "adAm"? It would become AdAm!

Is there a way to convert the rest of the text to lowercase?

Thanks
0
 
LVL 9

Accepted Solution

by:
dustock earned 500 total points
ID: 40395639
Yeah, not a problem, you just need to add a .ToLower() to the second part like below.

textBox1.Text = textBox1.Text[0].ToString().ToUpper() + textBox1.Text.Substring(1).ToLower();

Open in new window

0
 

Author Closing Comment

by:adamtrask
ID: 40395693
Thank you so much.
0
 
LVL 9

Expert Comment

by:dustock
ID: 40395696
No problem, glad I could help.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

792 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question