6

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
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
walk.this_way
  • 169
  • 1
  • 2
  • 11

7 Answers7

12

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
Mike Dinescu
  • 54,171
  • 16
  • 118
  • 151
  • thanks for answer but when i try it, the word is not right, if im try to write a 'notebook' , the word be 'koobeton' . im put the code in textbox1 keypress , – walk.this_way Dec 31 '13 at 16:41
  • if you want to perform the change on the fly (ie. as the user types the text) than you will also need to manipulate the cursor. I'll update the answer in a second.. – Mike Dinescu Dec 31 '13 at 16:48
7

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)

Top Systems
  • 951
  • 12
  • 24
  • Can you include a link to this? I can't locate it in the NetFramework docs. – dgo Nov 22 '19 at 15:01
  • 1
    @dgo [TextInfo.ToTitleCase Method](https://learn.microsoft.com/en-us/dotnet/api/system.globalization.textinfo.totitlecase?view=netframework-4.8) – Top Systems Nov 25 '19 at 10:53
  • Thanks for this. I read that this was bad practice though. Probably better to use CSS in my case. Thanks – dgo Nov 25 '19 at 17:01
1

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.

Community
  • 1
  • 1
Mike M
  • 1,382
  • 11
  • 23
1
' 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"
UDon
  • 11
  • 1
0

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
Adam
  • 147
  • 1
  • 13
0

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
Predator
  • 1
  • 1
-1

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
Jeff Mergler
  • 1,384
  • 20
  • 27