Solved

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

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

705 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now