Solved

Convert first letter in a string to capital

Posted on 2014-10-21
5
117 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

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.

803 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