Results 1 to 6 of 6

Thread: combobox keypress event

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2011
    Posts
    3

    Unhappy combobox keypress event

    I'm new to VB, and I need some help. I'm trying to get the 'Enter' key to behave like the tab key in a combobox control. I've placed the following code in the keypress event:

    If e.KeyChar = ToChar(Keys.Enter) Then
    NextTextBox.Focus()
    End If

    When ran, nothing happens. I've placed a break point at the 'private sub' line, and the code is not breaking. What am I doing wrong?

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: combobox keypress event

    Welcome to the forums.

    This works just fine for me
    vb.net Code:
    1. Private Sub ComboBox1_KeyPress(ByVal sender As Object, _
    2. ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles ComboBox1.KeyPress
    3.         If Asc(e.KeyChar) = Keys.Enter Then
    4.             TextBox1.Focus()
    5.         End If
    6. End Sub

  3. #3
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: combobox keypress event

    If the breakpoint isn't being hit, then the event isn't being called. What does your entire code block look like? Does it have the Handles on the end of it? compare it to lines 1 & 2 in Hack's sample... if the Handles part is missing, then that's the problem. Did you by any chance cut the combobox from the form and then put it back?

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  4. #4

    Thread Starter
    New Member
    Join Date
    Jan 2011
    Posts
    3

    Re: combobox keypress event

    Thank you for your response(s)

    I added the event handler by clicking on the keypress event in the properties/events window. So the IDE added the code. I verified that the syntax was correct with 'handles mycombobox.keypress' at the end.

    This is curious: when I use keydown instead of the keypress, it recognizes the break point.

  5. #5
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: combobox keypress event

    I have had situations where the KeyUp event was not happening, but I have never heard of the keypress event being skipped.

    By the way, could you be intercepting the keypress event at the form level? What I mean by this is that you could have KeyPreview set to true for the form, and be intercepting the keypress and suppressing the control level keypress. Of course, that's a bit improbable, but we might as well rule it out.
    My usual boring signature: Nothing

  6. #6

    Thread Starter
    New Member
    Join Date
    Jan 2011
    Posts
    3

    Re: combobox keypress event

    Hey Shaggy Hiker - I checked the key preview property and it is set false.

    In the interest of moving forward with this project, I'm going to code around this for now, and revisit later. There must be some quirk that I'm not grasping. But thanks for your input...

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