Results 1 to 7 of 7

Thread: how to convert into Upper case in keypress event

  1. #1

    Thread Starter
    Member
    Join Date
    Jul 2001
    Location
    Canada
    Posts
    36

    how to convert into Upper case in keypress event

    Hi,

    In vb6, we can use KeyAscii = Asc(UCase(Chr(KeyAscii))) in the keypress event to convert any char into its uppercase when the key is pressed.

    How to do this in vb.Net?

    Please give me a help.


    Thanks,

    James

  2. #2
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950
    Try e.keychar = UCase(e.keychar).
    I use e.keychar to check if a keychar = some value, but haven't tried this out.

  3. #3

    Thread Starter
    Member
    Join Date
    Jul 2001
    Location
    Canada
    Posts
    36

    It doesn't work

    Hi salvelinus


    It give me error message : keychar is read only.
    Any help?

    Thanks,

    James

  4. #4
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    If you want all of the text uppercase then you can use TextBox1.Text=TextBox1.Text.ToUpper which you could run in the KeyPress or in Validate. I believe the Char type has a ToUpper method as well so this would probably work too; e.keychar.ToUpper.

  5. #5
    Frenzied Member Mike Hildner's Avatar
    Join Date
    Jul 2002
    Location
    Des Moines, NM
    Posts
    1,690
    I've used this before for a mobile app. It works fine, but right now I can't remember why I had to set the position and handled etc. - maybe it's not the best code...

    VB Code:
    1. Private Sub TextBoxDOB_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBoxDOB.KeyPress
    2.         If e.KeyChar.IsLower(e.KeyChar) Then
    3.             ' Save current location
    4.             Dim pos As Integer = TextBoxDOB.SelectionStart
    5.             TextBoxDOB.Text = TextBoxDOB.Text & e.KeyChar.ToUpper(e.KeyChar)
    6.             TextBoxDOB.SelectionStart = pos + 1
    7.             e.Handled = True
    8.         End If
    9.     End Sub

  6. #6
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    If you're using a TxtBox there is a property that puts all the letters in uppercase
    \m/\m/

  7. #7
    Frenzied Member Mike Hildner's Avatar
    Join Date
    Jul 2002
    Location
    Des Moines, NM
    Posts
    1,690
    You're right, CharacterCasing is the property. FWIW, this property is not available in the Compact Framework, hence my funky work-around.

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