Results 1 to 6 of 6

Thread: Validating KeyPress

  1. #1

    Thread Starter
    Hyperactive Member Bananafish's Avatar
    Join Date
    Jan 2001
    Posts
    394

    Validating KeyPress

    I wish to Validate entry in a textbox so that only uppercse text is entered. If Lowercase text i sentered I wish to convert it automatically to uppercase.

    In vb6 - I could use the keypress event with keyascii.

    For example - to convert to uppercase.
    VB Code:
    1. If KeyAscii >= 97 And KeyAscii <= 122 Then
    2.             KeyAscii = KeyAscii - 32
    3.          End If

    Looking at .net - I can do something similar,

    VB Code:
    1. Private Sub txtJobCode_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtJobCode.KeyPress
    2.       If e.KeyChar.IsLower(e.KeyChar) Then
    3.          e.KeyChar.ToUpper(e.KeyChar)
    4.       End If
    5.  
    6.    End Sub

    but I can't figure out how to reset the key that was pressed (The above example, doesnt change what was entered in the text box).

    any ideas?

  2. #2
    Addicted Member ender_pete's Avatar
    Join Date
    Aug 2000
    Location
    Virginia, United States
    Posts
    131
    well i ran into the same type of problem, but if all you want to do is change the text to upper case do this after they tab away or in the changetext event because the keychar, keycode, keyascii object are read only so you cant change them

    Code:
    txtTest.Text = txtTest.Text.ToUpper
    ender_pete
    C#,VS.NET Ent Arch, vb6 ee sp5,html,vbscript,jscript,
    xml,dhtml,delphi,c++,vc++,java,cgi,php, python, ada(so ancient) ,adasage(also ancient) and others i can't remember.....

  3. #3
    Frenzied Member Jop's Avatar
    Join Date
    Mar 2000
    Location
    Amsterdam, the Netherlands
    Posts
    1,986
    easy, just set the e.Handled to true and the system will discard the KeyPress...
    for example this would block the user from entering the character c:
    VB Code:
    1. Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
    2.         If e.KeyChar = "c" Then e.Handled = True
    3.     End Sub


    ... hmm now that I re-read your post I'm not sure if this is what you actually meant
    Jop - validweb.nl

    Alcohol doesn't solve any problems, but then again, neither does milk.

  4. #4

    Thread Starter
    Hyperactive Member Bananafish's Avatar
    Join Date
    Jan 2001
    Posts
    394
    Thanks for the replies jop and ender_pete

    I did it as ender_pete suggested in the end (in the textchanged event), although I found I had to save the position of the selectionstart property and reset it afterwards.

    VB Code:
    1. Dim intX As Int32 = txtJobCode.SelectionStart
    2.       txtJobCode.Text = txtJobCode.Text.ToUpper()
    3.       txtJobCode.SelectionStart = intX

    Jops suggetion was useful for disallowing any character that isn't a Letter or Backspace...


    VB Code:
    1. If Not e.KeyChar = ControlChars.Back Then
    2.          If Not e.KeyChar.IsLetter(e.KeyChar) Then
    3.             Beep()
    4.             e.Handled = True
    5.          End If
    6.       End If

    Thanks guys

  5. #5
    Lively Member
    Join Date
    Mar 2000
    Location
    Germany
    Posts
    84

    Arrow

    there's also a new property for textboxes called CharacterCasing
    it can be set to normal, upper or lower.

    if set to upper, the textbox will automatically change lower case letters to uppercase

  6. #6

    Thread Starter
    Hyperactive Member Bananafish's Avatar
    Join Date
    Jan 2001
    Posts
    394
    Thanks Nina,

    I should have known there was an easy .Net way of doing it.

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