Reputation: 12874
I want my TextBox
to make the text I enter as Sentence Case(ProperCase)
.. but I don't want to write any code in an event like Lost Focus
or KeyPress
.
Just by default, whenever a user enters or types in a textbox the first letter of every word should automatically be converted into UpperCase
.
Upvotes: 2
Views: 17201
Reputation: 1
This case there are two controls: cboDestination and txtUser. It make what you want while you type letters.
Private Sub properCase_TextChanged(sender As Object, e As System.EventArgs) _
Handles cboDestination.TextChanged, txtUser.TextChanged
Dim n As Integer = sender.SelectionStart
sender.Text = StrConv(sender.Text, VbStrConv.ProperCase)
sender.SelectionStart = n
End Sub
Upvotes: 0
Reputation: 12589
I don't know of a way to do this in WinForms without putting some code in an event. The CharacterCasing
property of a TextBox allows you to force all characters entered to upper or lower case, but not to do Proper Casing. Incidentally, it's a single line of code to do it in an event:
TextBox1.Text = StrConv(TextBox1.Text, VbStrConv.ProperCase)
A more generic handler for doing this across multiple textboxes involves attaching a number of events to the same code:
'Attach multiple events to this handler
Private Sub MakeProperCase(sender As Object, e As EventArgs) Handles _
TextBox1.LostFocus, TextBox2.LostFocus, TextBox3.LostFocus
'Get the caller of the method and cast it to a generic textbox
Dim currentTextBox As TextBox = DirectCast(sender, TextBox)
'Set the text of the generic textbox to Proper Case
currentTextBox.Text = StrConv(currentTextBox.Text, VbStrConv.ProperCase)
End Sub
In ASP.NET, you can do this without code; there's a CSS property called text-transform
, one of the values for this property is capitalize
. When applied to a text input element, it makes the first letter of each word uppercase.
Upvotes: 17