Click here to Skip to main content
15,911,762 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I want that as user type a sentence, Program should automatically correct it to ProperCase or Sentence Case. To achive this, I used vbStrConv.ProperCase in TextChanged Event of particular Text box. But problem is It doesn't allow to input upper Characters anywhere except starting of the word.

It should not change the case of Manually typed UPPER Characters, I mean how can I make user able to enter UPPER CASE Characters also ?

C# code is also accepted.

Thank you.
Posted

How about this piece of code?
C#
string proper = "TEST STRING";
CultureInfo properCase = System.Threading.Thread.CurrentThread.CurrentCulture;
TextInfo currentInfo = properCase.TextInfo;
proper = currentInfo.ToTitleCase(currentInfo.ToLower(proper));

Output:
C#
// proper = "Test String"


Also explore:
http://msdn.microsoft.com/en-us/library/system.globalization.textinfo.totitlecase.aspx[^]
http://www.bondigeek.com/blog/2011/01/17/titleproper-case-in-c/[^]
http://www.daniweb.com/software-development/csharp/code/306751/format-a-string-to-proper-case-with-c[^]
 
Share this answer
 
I've noticed that you want any letters already in UPPERcase to stay in Upper case. I can't think of anyway to do that unless you examine the string char by char. E.g.
VB
Dim sInput As String = "tesT strinG"
'Get a copy of the input in proper case
Dim sProperCase = System.Threading.Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase(sInput)
Dim sOutput As String = ""
'Now check for the original having upper case 
For i As Integer = 0 To sInput.Length - 1
    If Char.IsUpper(sInput.Substring(i, 1)) Then
        sOutput += sInput.Substring(i, 1) 'use the original string if the original was upper case
    Else
        sOutput += sProperCase.Substring(i, 1) 'use the copy that was converted to proper case
    End If
Next

Output ...
TesT StrinG
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900