Solved

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

Posted on 2004-09-08
5
225 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
  • 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Gridview selected row 9 46
Get the selected ValueMember of Combobox 5 30
How to sort a list of objects using an item in an enclosed list 5 46
SqlServer no dupes 25 34
This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

777 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