|
|
Title | Convert the text the user is typing in a TextBox to Proper Case in Visual Basic .NET |
Description | This example shows how to convert the text the user is typing in a TextBox to Proper Case in Visual Basic .NET |
Keywords | TextBox, convert, proper case, StrConv, VB.NET |
Categories | Controls, Miscellany, Strings |
|
|
This example has three TextBoxes that convert their text into UPPER CASE, lower case, and Proper Case. Each control's Change event handler works in the same way. It stores the TextBox's SelStart and SelLength properties in local variables, converts the text as necessary, and restores the SelStart and SelLength values.
|
|
Private Sub txtUpperCase_TextChanged(ByVal sender As _
System.Object, ByVal e As System.EventArgs) Handles _
txtUpperCase.TextChanged
Dim sel_start As Integer
Dim sel_length As Integer
sel_start = txtUpperCase.SelectionStart
sel_length = txtUpperCase.SelectionLength
txtUpperCase.Text = txtUpperCase.Text.ToUpper
txtUpperCase.SelectionStart = sel_start
txtUpperCase.SelectionLength = sel_length
End Sub
Private Sub txtLowerCase_TextChanged(ByVal sender As _
System.Object, ByVal e As System.EventArgs) Handles _
txtLowerCase.TextChanged
Dim sel_start As Integer
Dim sel_length As Integer
sel_start = txtLowerCase.SelectionStart
sel_length = txtLowerCase.SelectionLength
txtLowerCase.Text = txtLowerCase.Text.ToLower
txtLowerCase.SelectionStart = sel_start
txtLowerCase.SelectionLength = sel_length
End Sub
Private Sub txtProperCase_TextChanged(ByVal sender As _
System.Object, ByVal e As System.EventArgs) Handles _
txtProperCase.TextChanged
Dim sel_start As Integer
Dim sel_length As Integer
sel_start = txtProperCase.SelectionStart
sel_length = txtProperCase.SelectionLength
txtProperCase.Text = StrConv(txtProperCase.Text, _
VbStrConv.ProperCase)
txtProperCase.SelectionStart = sel_start
txtProperCase.SelectionLength = sel_length
End Sub
|
|
Note that you can also set a TextBox's CharacterCasing property to CharacterCasing.Lower or CharacterCasing.Upper. See:
This is easier but a bit less flexible (for example, it cannot handle ProperCase).
|
|
|
|
|
|