Question

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
Was it helpful?

Solution

Don't use a static variable to hold the previous char. It's not needed and bad practice in general.

Then, although it's a bit unclear from your question, assuming you wish perform the change on the text of TextBox1, you will probably want to set the text back to the TextBox after changing it.

So a solution might looks like this:

 If TextBox1.TextLength > 1 Then
     TextBox1.Text = TextBox1.Text.Substring(0, 1).ToUpper() + TextBox1.Text.Substring(1)
 ElseIf TextBox1.TextLength = 1 Then
     TextBox1.Text = TextBox1.Text.ToUpper()
 EndIf

If you want to capitalize the first letter and force lowercase the rest you can modify the code above like so:

 If TextBox1.TextLength > 1 Then
     TextBox1.Text = TextBox1.Text.Substring(0, 1).ToUpper() + TextBox1.Text.Substring(1).ToLower()
 ElseIf TextBox1.TextLength = 1 Then
     TextBox1.Text = TextBox1.Text.ToUpper()
 EndIf

UPDATE

Based on the comments, if you want to make this change on the fly (ie. as the user is typing in the TextBox) then you will also need to manipulate the cursor. Essentially you need to store the cursor position prior to changing the text, and then restore the position after the change.

Also, I would perform these changes in the KeyUp event as opposed to the KeyPress event. The KeyUp happens after the TextBox has registered the change in response to the key press.

 Dim startPos as Integer
 Dim selectionLength as Integer

' store the cursor position and selection length prior to changing the text
 startPos = TextBox1.SelectionStart
 selectionLength = TextBox1.SelectionLength

 ' make the necessary changes
 If TextBox1.TextLength > 1 Then
     TextBox1.Text = TextBox1.Text.Substring(0, 1).ToUpper() + TextBox1.Text.Substring(1).ToLower()
 ElseIf TextBox1.TextLength = 1 Then
     TextBox1.Text = TextBox1.Text.ToUpper()
 EndIf

 ' restore the cursor position and text selection
 TextBox1.SelectionStart = startPos
 TextBox1.SelectionLength = selectionLength

OTHER TIPS

TextInfo.ToTitleCase Method Converts the specified string to title case.

txtName.Text = Threading.Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase(txtName.Text.ToLower)

You have to convert the whole text to lowercase first or else it won't work as expected:

(except for words that are entirely in uppercase, which are considered to be acronyms)

You definitely can do it.

RegularExpressions are the most direct way...similar to what you're doing, and you can combine them.

Plenty of people have already looked at this. Here's one:
Formatting sentences in a string using C#

Use that regular expression string replacement instead of your Char.ToUpper.
Or just wait until all the text is entered and run it once, such as when the control loses focus.

' 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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top