Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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
like image 633
walk.this_way Avatar asked Dec 31 '13 16:12

walk.this_way


2 Answers

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
like image 125
Mike Dinescu Avatar answered Oct 01 '22 04:10

Mike Dinescu


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)

like image 43
Top Systems Avatar answered Oct 01 '22 02:10

Top Systems