Solved

Convert first letter in a string to capital

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
This video discusses moving either the default database or any database to a new volume.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

747 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now