Results 1 to 10 of 10

Thread: Trap double quotes in KeyDown [Resolved]

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950

    Trap double quotes in KeyDown [Resolved]

    I want to trap the keystroke for double quotes and prevent the user from entering it in some text boxes. In VBA, the code would be like this:
    VB Code:
    1. If KeyCode = 34 Then
    2.     DoCmd.CancelEvent
    After searching the forum, I came up with this for the vb.net version:
    VB Code:
    1. Private Sub txtModName_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtModName.KeyDown
    2.         If Asc(e.KeyCode) = Keys.OemQuotes Then
    3.             e.Handled = False
    4.         End If
    5.     End Sub
    But it doesn't work. When I put a breakpoint in and check the e.keycode, it shows the Shift key used to get the doublequotes; oemquotes is 222. How can I trap this? Thanks.
    Last edited by salvelinus; Dec 16th, 2003 at 11:06 AM.

  2. #2
    Hyperactive Member
    Join Date
    Mar 2002
    Location
    Dublin (Ireland)
    Posts
    304
    use the Key Press event and set e.handled to true will ignore the "

  3. #3
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    This will work .
    VB Code:
    1. Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
    2.         If e.Shift = True And e.KeyValue = 222 Then
    3.             MessageBox.Show("You pressed Shift + Quote button")
    4.           'Or handle it the way you like
    5.         End If
    6. End Sub

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950
    Well, that doesn't work. e.shift = true, but e.keyvalue = 16, which is the "data link escape" character, whatever that means. This is a standard keyboard.
    KeyPress doesn't intercept the Shift character like KeyDown does, but it doesn't have the same properties.
    There's another weird problem with the textboxes, maybe they're related, though I don't see how. I get the user name via API, it shows fine in a msgbox, but won't show in the textbox, even though setting a breakpoint and checking the value if text1.text shows the value there.

  5. #5
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Impossible . Try it here .
    Attached Files Attached Files

  6. #6

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950
    I get an error "Not a valid solution file" when I try to open the uncompressed file. The code .vb file opens ok, but no design .vb
    I'm using .net 7.0, if that makes a difference, and keypreview is set to true on the form.
    I think what's happening is that the keydown event first catches the shift press, so e.shift is true, but it doesn't catch the e.keyvalue till the next press, where shift is no longer in the keydown event. At this point, e.keyvalue does equal 222. Anyway, I've tried different variations, and the quotes still display.
    This is a minor part of the program, could probably skip it, but it bugs me.
    Last edited by salvelinus; Dec 16th, 2003 at 10:11 AM.

  7. #7
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    The attached project is VB.NET 2003 . Basically , you need VB.NET 2003 to run the proj or get it converted with the tool in my signature . Concering the keyvalue of DQoutes , which I got it by different project (look the attachement) , I showed you how to detect while these combination are being pressed , then it's your part to handle them .
    Attached Images Attached Images  

  8. #8

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950

    VICTORY!

    Thanks for all your help, Pirate, I really appreciate it. I solved it a different way:
    VB Code:
    1. Private Sub txtModName_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtModName.KeyPress
    2.         If e.KeyChar = Chr(34) Then
    3.             e.Handled = True
    4.         End If
    5.     End Sub
    Now on to seeing why the username doesn't show in the textbox, even though putting a watch on the text property in code shows it there.

  9. #9
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Good it's working now .

    I can't guess why it's not showing since I didn't see the code but initially store the retrieved value in a string variable and use the variable into the messagebox .

  10. #10
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    What username are you trying to get ?

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