none
Keypress event doesn't work on textbox if AutoComplete set on it.

    Question

  • I have my program set up to monitor several textboxes so that if the enter key has been pressed it runs a subroutine.



    Private Sub TextBox_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress, _
        TextBox2.KeyPress, TextBox3.KeyPress, TextBox4.KeyPress, TextBox5.KeyPress
            If (e.KeyChar = Convert.ToChar(13)) Then
                Button1.PerformClick()
                e.Handled = True
            End If
        End Sub

     


    This code works fine when the AutocComplete isn't set on the TextBox that I press the "Enter" key on. I even put a break point on this sub to check if the event even occurs and no go. The event doesn't even fire. It just highlights the text in the TextBox. By all rights it should "Stop" every time I press a key in the TextBox. So I imagine the AutoComplete has stolen the event for itself and not passed it on.

    Got any ideas on how I might get the AutoComplete to work while i monitor the keypress? 
    Wednesday, November 02, 2005 11:10 PM

Answers

  • Hi,

    I just tried it in VS2005 and it just works fine. Have you tried capturing the keypress on the form instead on the textbox? Try setting the Form's KeyPreview property to true then handle the form's Keypress event. This tends to let the form capture all keystrokes made to any controls in the form...


    cheers,

    Paul June A. Domag
    Thursday, November 03, 2005 1:48 AM

All replies

  • Hi,

    I just tried it in VS2005 and it just works fine. Have you tried capturing the keypress on the form instead on the textbox? Try setting the Form's KeyPreview property to true then handle the form's Keypress event. This tends to let the form capture all keystrokes made to any controls in the form...


    cheers,

    Paul June A. Domag
    Thursday, November 03, 2005 1:48 AM
  • OK, I was just experimenting with the feature and had set the Mode to "Suggest" and the Source to "History" to see what it does. This is where it happens and still does (seems to do the same thing on other sources too) but when I set the source to "Custom" which I'll be using, it works fine. I was looking for the way to make the form accept the keystroke as well... thank you a hundred times!! Didn't seem right to have to assign all the TextBoxes the event procedure.
    Thursday, November 03, 2005 1:09 PM
  •  

    I am suffering from the same problem. But I find that the problem is not from the keypress event, the keypress event still works except the "enter" key.

     

     

    You will find this by the following simple program:

     

    Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress   

                 MessageBox.Show(e.KeyChar)

    End Sub

     

    Anyone knows how to deal with the problem? Thanks so much!

    Monday, May 19, 2008 12:03 PM
  • Remove the code from your TextBox_KeyPress and paste it in the PreviewKeyDown event. It should work fine then even if the autocomplete is enabled, I have found that the keyup and keydown events will not work if the autocomplete is set and the User presses enter



    I'm new to .Net, OOP and this forum but love it!

    Wednesday, May 22, 2013 2:55 AM