Hello @Gary Simpson
Even though you have a solution you might also consider using a custom TextBox. There are two properties shown below to convert .Text to title case. The OverrodeUpperCased needs to be set if you have something like KAREN PAYNE while not for karen payne.
Source code
Imports System.ComponentModel
Imports System.Globalization
Namespace Controls
''' <summary>
''' * Suppress beep on ENTER key pressed
''' * Provides ability to title case text
''' </summary>
Public Class NoBeepTitleCaseTextBox
Inherits TextBox
Public Delegate Sub TriggerDelegate()
''' <summary>
''' Subscribe to be notified when ENTER was pressed.
''' </summary>
Public Event TriggerEvent As TriggerDelegate
Protected Overrides Sub OnKeyDown(ByVal e As KeyEventArgs)
If e.KeyCode = Keys.Enter Then
e.Handled = True
e.SuppressKeyPress = True
If ToTitleCase Then
HandleCasing()
End If
TriggerEventEvent?.Invoke()
Return
End If
MyBase.OnKeyDown(e)
End Sub
''' <summary>
''' Use to transform .Text to title case.
''' </summary>
Private Sub HandleCasing()
If useTitleCase Then
Dim textInfo As TextInfo = (New CultureInfo("en-US", False)).TextInfo
If uppercaseOption Then
Text = Text.ToLower()
End If
Text = textInfo.ToTitleCase(Text)
SelectionStart = Text.Length
SelectionLength = 0
End If
End Sub
Private useTitleCase As Boolean
Private uppercaseOption As Boolean
<Browsable(True)>
<Category("Extended Properties")>
<Description("Set text to title case")>
<DisplayName("ToTitleCase")>
Public Property ToTitleCase() As Boolean
Get
Return useTitleCase
End Get
Set
useTitleCase = Value
End Set
End Property
<Browsable(True)>
<Category("Extended Properties")>
<Description("Override casing when all text may be uppercased")>
<DisplayName("OverrideUpperCased")>
Public Property OverrideUpperCased() As Boolean
Get
Return uppercaseOption
End Get
Set
uppercaseOption = Value
End Set
End Property
''' <summary>
''' We can not trap TAB in OnKeyDown so handle it here
''' then permit default via MyBase.ProcessCmdKey
''' </summary>
''' <param name="msg"></param>
''' <param name="keyData"></param>
''' <returns></returns>
Protected Overrides Function ProcessCmdKey(ByRef msg As Message, ByVal keyData As Keys) As Boolean
If keyData = Keys.Tab Then
If ToTitleCase Then
HandleCasing()
End If
End If
Return MyBase.ProcessCmdKey(msg, keyData)
End Function
End Class
End Namespace
Test code
Public Class MainForm
Private Sub UseTitleCaseCheckBox_CheckedChanged(sender As Object, e As EventArgs) _
Handles UseTitleCaseCheckBox.CheckedChanged
NameTextBox1.ToTitleCase = UseTitleCaseCheckBox.Checked
NameTextBox2.ToTitleCase = UseTitleCaseCheckBox.Checked
End Sub
Private Sub OverrideCasingCheckBox_CheckedChanged(sender As Object, e As EventArgs) _
Handles OverrideCasingCheckBox.CheckedChanged
NameTextBox1.OverrideUpperCased = OverrideCasingCheckBox.Checked
NameTextBox2.OverrideUpperCased = OverrideCasingCheckBox.Checked
End Sub
End Class