?
Solved

How do I catch the Enter key on a combo box?

Posted on 2004-09-08
5
Medium Priority
?
233 Views
Last Modified: 2010-04-23
Hi!

     Here's what I've got:  A crystal report on a form with 2 combo boxes above it.  One is for serial #, one is for stock #.  I want the user to be able to select the serial or stock from their lists OR to just type them in and press enter.  The report should then be updated.  Here's what I've got:

(cmbSerial and cmbStock are the combo box names)


  Private Sub cmbSerial_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles MyBase.KeyPress
        If e.KeyChar = Chr(13) Then
            CrystalReportViewer1.SelectionFormula = "{custody_audit.serial_no} = '" & cmbSerial.Text & "'"
            CrystalReportViewer1.RefreshReport()
            Debug.WriteLine("Serial Enter press")
            e.Handled = True
        End If
    End Sub


    Private Sub cmbStock_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles MyBase.KeyPress
        If e.KeyChar = Chr(13) Then
            CrystalReportViewer1.SelectionFormula = "{custody_audit.stock_number} = '" & cmbStock.Text & "'"
            CrystalReportViewer1.RefreshReport()
            Debug.WriteLine("Stock Enter press")
            e.Handled = True
        End If
    End Sub

This does almost nothing - the only thing I can tell is that when I click on the drop-down button for the serial combo box, I get the debug line "Serial Enter press" - doing the same for the stock number box does nothing.  Hitting enter in either does nothing.  

Thanks!

JP

0
Comment
Question by:gleznov
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 

Author Comment

by:gleznov
ID: 12015398
Here's another way I'm trying to do it:

Protected Overrides Function ProcessCmdKey(ByRef msg As System.Windows.Forms.Message, ByVal keyData As System.Windows.Forms.Keys) As Boolean
        Const WM_KEYDOWN As Integer = &H100
        Const WM_SYSKEYDOWN As Integer = &H104
        Dim e As System.EventArgs
        Dim stk As Integer = 0
        Dim srl As Integer = 0

        If ((msg.Msg = WM_KEYDOWN) Or (msg.Msg = WM_SYSKEYDOWN)) Then
            Select Case (keyData)
                Case Keys.Enter
                    If cmbStock.Focus = True Then stk = 1
                    If cmbSerial.Focus = True Then srl = 1
                    Debug.WriteLine("Start of override:")
                    Debug.WriteLine("stk = " & stk & " & srl = " & srl)
                    stk = 0
                    srl = 0

                    '--------Do something here----------
                    If cmbStock.Focus = True Then
                        CrystalReportViewer1.SelectionFormula = "{custody_audit.stock_number} = '" & cmbStock.Text & "'"
                        CrystalReportViewer1.RefreshReport()
                        Debug.WriteLine("Stock Enter press")
                    Else
                        If cmbSerial.Focus = True Then
                            CrystalReportViewer1.SelectionFormula = "{custody_audit.serial_no} = '" & cmbSerial.Text & "'"
                            CrystalReportViewer1.RefreshReport()
                            Debug.WriteLine("Serial Enter press")
                        End If
                    End If
            End Select
        End If
        Return MyBase.ProcessCmdKey(msg, keyData)
    End Function


The problem here is that the debug.writelines show me that both combo boxes are in focus and whenever I hit enter, the field shifts from Serial to Stock.  if Stock is focused, it does not however switch back to serial.  cmbStock works, cmbSerial doesn't.  Why?

JP
0
 

Author Comment

by:gleznov
ID: 12015422
And I lied, stock box isn't working anymore either.

JP
0
 

Author Comment

by:gleznov
ID: 12015910
OK, I found a solution.  In case anyone's ever looking for something like this, let me post it:


 Protected Overrides Function ProcessDialogKey(ByVal keyData As System.Windows.Forms.Keys) As Boolean

        If Me.ActiveControl.Name = "cmbSerial" And keyData = Keys.Enter Then

            'the control is the button and the user has pressed Enter key, so handle this.
            SerialUpdate()
            Return False
        ElseIf Me.ActiveControl.Name = "cmbStock" And keyData = Keys.Enter Then

            StockUpdate()
            Return False

        Else

            'Otherwise, let the key be processed normally.
            Return MyBase.ProcessDialogKey(keyData)

        End If

    End Function

    Private Sub SerialUpdate()
        CrystalReportViewer1.SelectionFormula = "{custody_audit.serial_no} = '" & cmbSerial.Text & "'"
        CrystalReportViewer1.RefreshReport()
        cmbStock.Text = ""
    End Sub

    Private Sub StockUpdate()
        CrystalReportViewer1.SelectionFormula = "{custody_audit.stock_number} = '" & cmbStock.Text & "'"
        CrystalReportViewer1.RefreshReport()
        cmbSerial.Text = ""
    End Sub
0
 
LVL 1

Accepted Solution

by:
Computer101 earned 0 total points
ID: 12231815
PAQed, with points refunded (125)

Computer101
E-E Admin
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question