Results 1 to 5 of 5

Thread: RESOLVED How to use Propercase?

  1. #1

    Thread Starter
    Addicted Member Sanko's Avatar
    Join Date
    Mar 2000
    Location
    Argentina
    Posts
    128

    Resolved 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:
    1. Public Function Txtmayus(Texto As TextBox, KeyAscii As Integer)
    2. ' SI EL PRIMER CARACTER ES UN ESPACIO
    3. If Len(Texto) = 0 Then
    4.     If KeyAscii = 32 Then
    5.         ' CANCELAR
    6.         KeyAscii = 0
    7.         Exit Function
    8.     End If
    9. End If
    10. ' SI NO ES EL 1° CARACTER
    11. If Texto.SelStart <> 0 Then
    12.     ' DESPUES DEL ESPACIO NO
    13.     If Mid$(Texto.Text, Texto.SelStart, 1) <> " " Then
    14.         ' NO MAS DE 2 ESPACIOS CONSECUTIVOS
    15.         If KeyAscii = 32 Then KeyAscii = 0
    16.     End If
    17. End If
    18. KeyAscii = Asc(UCase$(Chr$(KeyAscii)))
    19. End Function
    And in the Keypress event of a Textbox
    VB Code:
    1. 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:
    1. ' Permito solo Caracteres
    2.     Public Sub TxtMayus(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
    3.         Dim Texto As TextBox = CType(sender, TextBox)
    4.  
    5.              ' Si el primer caracter es un espacio
    6.         If Len(Texto.Text) = 0 Then
    7.             If Asc(e.KeyChar) = 32 Then
    8.                 ' Lo anulo
    9.                 e.Handled = True
    10.                 Exit Sub
    11.             End If
    12.         End If
    13.         ' Si no es el 1° caracter
    14.         If Texto.SelectionStart <> 0 Then
    15.             ' Despues del espacio no            
    16.             If Mid(Texto.Text, Texto.SelectionStart, 1) = " " Then
    17.                 ' No mas de 2 espacios consecutivos
    18.                 If Asc(e.KeyChar) = 32 Then
    19.                     e.Handled = True
    20.                     Exit Sub
    21.                 End If
    22.             End If
    23.         End If
    24.         Texto.Text = StrConv(Texto.Text, VbStrConv.ProperCase)
    25.     End Sub
    And i try to use SendKeys.send but the app Blocks, hang
    VB Code:
    1. e.Handled = True
    2. SendKeys.Send(e.KeyChar.ToUpper(e.KeyChar))

    Any idea??
    Last edited by Sanko; Sep 27th, 2005 at 09:03 PM.

  2. #2
    Frenzied Member Zakary's Avatar
    Join Date
    Mar 2005
    Location
    Canada, Quebec, Montreal
    Posts
    1,654

    Re: How to use Propercase in keypress?

    Try to insert manualy with something like
    VB Code:
    1. 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
    Using VS 2010 on Fw4.0

  3. #3

    Thread Starter
    Addicted Member Sanko's Avatar
    Join Date
    Mar 2000
    Location
    Argentina
    Posts
    128

    Re: How to use Propercase in keypress?

    Thanks to Zakary i modified the code

    VB Code:
    1. ' Si el primer caracter es un espacio
    2.         If Texto.Text.Length = 0 Then
    3.             If Asc(e.KeyChar) = 32 Then
    4.                 ' Lo anulo
    5.                 e.Handled = True
    6.                 Exit Sub
    7.             End If
    8.             ' Obtengo el primer caracter
    9.             e.Handled = True
    10.             Texto.Text = Texto.Text & e.KeyChar.ToUpper(e.KeyChar)
    11.             Texto.SelectionStart = Len(Texto.Text) + 1
    12.         End If
    13.  
    14.         ' Si no es el 1° caracter
    15.         If Texto.SelectionStart <> 0 Then
    16.             ' Despues del espacio no                                    
    17.             If Texto.Text.Substring(Texto.Text.Length - 1, 1) = " " Then
    18.                 ' No mas de 2 espacios consecutivos
    19.                 If Asc(e.KeyChar) = 32 Then
    20.                     e.Handled = True
    21.                     Exit Sub
    22.                 End If
    23.                 ' Backspace
    24.                 If Asc(e.KeyChar) = 8 Then
    25.                     Exit Sub
    26.                 End If
    27.                 e.Handled = True
    28.                 Texto.Text = Texto.Text & e.KeyChar.ToUpper(e.KeyChar)
    29.                 Texto.SelectionStart = Len(Texto.Text) + 1
    30.             End If
    31.         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

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: How to use Propercase in keypress?

    VB Code:
    1. Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
    2.         Dim selectionStart As Integer = Me.TextBox1.SelectionStart
    3.  
    4.         Me.TextBox1.Text = Globalization.CultureInfo.CurrentCulture.TextInfo.ToTitleCase(Me.TextBox1.Text)
    5.  
    6.         If selectionStart <> -1 Then
    7.             Me.TextBox1.SelectionStart = selectionStart
    8.         End If
    9.     End Sub
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Addicted Member Sanko's Avatar
    Join Date
    Mar 2000
    Location
    Argentina
    Posts
    128

    Resolved Re: How to use Propercase in keypress?

    Thank You jmcilhinney that is what i need

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width