RESOLVED How to use Propercase?
If you dont know Propercase "(first char of a word is Upercase --> Visual Basic Is Easy)"
Hi i have an old Propercase function in VB6, but i cant apply in Vb.Net
This function changes to Propercase in realtime when i write text in the textbox using the keypress.
here is the VB6 way:
VB Code:
Public Function Txtmayus(Texto As TextBox, KeyAscii As Integer)
' SI EL PRIMER CARACTER ES UN ESPACIO
If Len(Texto) = 0 Then
If KeyAscii = 32 Then
' CANCELAR
KeyAscii = 0
Exit Function
End If
End If
' SI NO ES EL 1° CARACTER
If Texto.SelStart <> 0 Then
' DESPUES DEL ESPACIO NO
If Mid$(Texto.Text, Texto.SelStart, 1) <> " " Then
' NO MAS DE 2 ESPACIOS CONSECUTIVOS
If KeyAscii = 32 Then KeyAscii = 0
End If
End If
KeyAscii = Asc(UCase$(Chr$(KeyAscii)))
End Function
And in the Keypress event of a Textbox
VB Code:
Txtmayus(Text1, Keyascii)
But in .Net the e.KeyChar is read only and i cant asign a value, and if i use
Texto.Text = StrConv(Texto.Text, VbStrConv.ProperCase) it reverses the chars.
Here is my VB.net code
VB Code:
' Permito solo Caracteres
Public Sub TxtMayus(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
Dim Texto As TextBox = CType(sender, TextBox)
' Si el primer caracter es un espacio
If Len(Texto.Text) = 0 Then
If Asc(e.KeyChar) = 32 Then
' Lo anulo
e.Handled = True
Exit Sub
End If
End If
' Si no es el 1° caracter
If Texto.SelectionStart <> 0 Then
' Despues del espacio no
If Mid(Texto.Text, Texto.SelectionStart, 1) = " " Then
' No mas de 2 espacios consecutivos
If Asc(e.KeyChar) = 32 Then
e.Handled = True
Exit Sub
End If
End If
End If
Texto.Text = StrConv(Texto.Text, VbStrConv.ProperCase)
End Sub
And i try to use SendKeys.send but the app Blocks, hang :eek2:
VB Code:
e.Handled = True
SendKeys.Send(e.KeyChar.ToUpper(e.KeyChar))
Any idea?? :confused:
Re: How to use Propercase in keypress?
Try to insert manualy with something like
VB Code:
MyTextBox.Text.Insert(CharPosition, CharToInsert)
To make you code more in .NET philosophy;
-Instead of using the Len function you may use MyTextBox.Text.Length
-instead of usgin the Mid function you may use MyTextBox.Text.Substring
Hope this will help :)
Re: How to use Propercase in keypress?
Thanks to Zakary i modified the code
VB Code:
' Si el primer caracter es un espacio
If Texto.Text.Length = 0 Then
If Asc(e.KeyChar) = 32 Then
' Lo anulo
e.Handled = True
Exit Sub
End If
' Obtengo el primer caracter
e.Handled = True
Texto.Text = Texto.Text & e.KeyChar.ToUpper(e.KeyChar)
Texto.SelectionStart = Len(Texto.Text) + 1
End If
' Si no es el 1° caracter
If Texto.SelectionStart <> 0 Then
' Despues del espacio no
If Texto.Text.Substring(Texto.Text.Length - 1, 1) = " " Then
' No mas de 2 espacios consecutivos
If Asc(e.KeyChar) = 32 Then
e.Handled = True
Exit Sub
End If
' Backspace
If Asc(e.KeyChar) = 8 Then
Exit Sub
End If
e.Handled = True
Texto.Text = Texto.Text & e.KeyChar.ToUpper(e.KeyChar)
Texto.SelectionStart = Len(Texto.Text) + 1
End If
End If
The only problem is found if you write some word an then place the cursor in the begin that char is lowercase when it must be Uppercase example
write keyboard the text shows Keyboard now put the cursor in the begin and write aa it shows aaKeyboard it must say Aakeyboard :rolleyes:
Re: How to use Propercase in keypress?
VB Code:
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
Dim selectionStart As Integer = Me.TextBox1.SelectionStart
Me.TextBox1.Text = Globalization.CultureInfo.CurrentCulture.TextInfo.ToTitleCase(Me.TextBox1.Text)
If selectionStart <> -1 Then
Me.TextBox1.SelectionStart = selectionStart
End If
End Sub
Re: How to use Propercase in keypress?
:thumb: Thank You jmcilhinney that is what i need