Friend Const gstrValidChars As String = "abcdefghijklmnopqrstuvwxyz'- ABCDEFGHIJKLMNOPQRSTUVWXYZ"
Friend Sub KeyPressName(ByRef Sender As Object, ByRef e As System.Windows.Forms.KeyPressEventArgs, ByVal AllowAllChars As Boolean)
Dim TextBox As TextBox = CType(Sender, TextBox)
Dim strLastChar As String
'Handles all the name fields in the app
If Asc(e.KeyChar) <> Keys.Back Then
If AllowAllChars = False And InStr(gstrValidChars, e.KeyChar) = 0 Then
'If an invalid char is passed then its dropped
e.Handled = True
Else
If e.KeyChar.IsLetter(e.KeyChar) = True Then
TextBox.SelectedText = ""
If TextBox.TextLength <> 0 Then
strLastChar = Mid(TextBox.Text, TextBox.TextLength, 1)
Else
strLastChar = "'"
End If
If strLastChar = " " Or strLastChar = "-" Or strLastChar = "'" Then
TextBox.SelectedText = e.KeyChar.ToUpper(e.KeyChar)
e.Handled = True
End If
End If
End If
End If
End Sub
Private Sub TextBox1_KeyPress (...) Handles TextBox1.KeyPress
Call KeyPressName(sender, e, False) 'do not allow all characters
End Sub