How to capitalize letter only in beginning of sentences, and the next word is normal

Viewed 33542

I use this:

Static PreviousLetter As Char
    If PreviousLetter = " "c Or TextBox1.Text.Length = 0 Then
        e.KeyChar = Char.ToUpper(e.KeyChar)
    End If
    PreviousLetter = e.KeyChar

But the result is always:

Good Night Every Body

How can I capitalize just the first letter in the sentence, leaving the other words normal? The result I want is:

Good night every body
7 Answers
' With VB.net you could use the Mid() function also for assignments

Dim s as string = "good evening" 
If Len(s)>0 Then  Mid(s, 1, 1) = UCase(Left(s, 1)) 

'Result: "Good evening"

Try this

    Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged

    TextBox1.Text = System.Threading.Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase(TextBox1.Text)
    TextBox1.Select(TextBox1.Text.Length, 0)

    End Sub

create an extension:

Public Function ToUppercaseFirstLetter(ByVal Text As String) As String
    If Text = "" Then Return Text
    Dim array() As Char = Text.ToCharArray
    array(0) = Char.ToUpper(array(0))
    Return array
End Function

try this code

    If Char.IsLetter(e.KeyChar) Then

        If txtType.Text.Length = 0 Then
            e.KeyChar = Char.ToUpper(e.KeyChar)
        Else
            e.KeyChar = Char.ToLower(e.KeyChar)
        End If

    End If
Related