• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 243
  • Last Modified:

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

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
gleznov
Asked:
gleznov
  • 3
1 Solution
 
gleznovAuthor Commented:
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
 
gleznovAuthor Commented:
And I lied, stock box isn't working anymore either.

JP
0
 
gleznovAuthor Commented:
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
 
Computer101Commented:
PAQed, with points refunded (125)

Computer101
E-E Admin
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now