|
|
Title | Convert the text the user is typing in a TextBox to Proper Case |
Description | This example shows how to convert the text the user is typing in a TextBox to Proper Case in Visual Basic 6. |
Keywords | TextBox, convert, proper case, StrConv |
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.
The routines use StrConv to convert the text to UPPER CASE, lower case, or Proper Case.
|
|
Private Sub txtLowerCase_Change()
Dim sel_start As Integer
Dim sel_length As Integer
sel_start = txtLowerCase.SelStart
sel_length = txtLowerCase.SelLength
txtLowerCase.Text = StrConv(txtLowerCase.Text, _
vbLowerCase)
txtLowerCase.SelStart = sel_start
txtLowerCase.SelLength = sel_length
End Sub
Private Sub txtProperCase_Change()
Dim sel_start As Integer
Dim sel_length As Integer
sel_start = txtProperCase.SelStart
sel_length = txtProperCase.SelLength
txtProperCase.Text = StrConv(txtProperCase.Text, _
vbProperCase)
txtProperCase.SelStart = sel_start
txtProperCase.SelLength = sel_length
End Sub
Private Sub txtUpperCase_Change()
Dim sel_start As Integer
Dim sel_length As Integer
sel_start = txtUpperCase.SelStart
sel_length = txtUpperCase.SelLength
txtUpperCase.Text = StrConv(txtUpperCase.Text, _
vbUpperCase)
txtUpperCase.SelStart = sel_start
txtUpperCase.SelLength = sel_length
End Sub
|
|
Note that you can set extended styles in a TextBox to make it produce UPPER CASE or lower case, or to allow only digits. In that case, the Change event handler isn't necessary, although it provide more flexibility (for example, you can implement Proper Case). See:
|
|
|
|
|
|