Group,

I need to use Function keys within a form I've created. The problem I'm having is determing the correct routine to fire it off. Here's what I'm attempting to do:

In Form1 (actually called "OrderEntry2"), I want to give the user the option of keying in a part number into the "txbPartNo" textbox. Or, if they don't know the part number, I want to allow them to hit 'F5' to bring up Form2 (called "Popup_PartNo) to find that part number based on the beginning characters that they will enter on that form.

Likewise, should the user not know the beginning characters of the part number, I'd want to give them the option of finding the part number based on a word within the part description. Again I want to allow them to hit 'F5' to bring up Form2 (called "Popup_PartNo) to perform the routine.

My first inclination was to allow the user to give FOCUS to either "txbPartNo" textbox or "txbDescription" textbox to choose which way they want to search. So I wrote the following:

Public Sub txbPartNo_GotFocus(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txbPartNo.GotFocus
        If e.KeyValue = Keys.F5 Then
            FuncKeysModule(e.KeyValue)
            e.Handled = True
        End If

    End Sub

    Public Sub txbDesc_GotFocus(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txbDesc.GotFocus
        If e.KeyValue = Keys.F5 Then
            FuncKeysModule(e.KeyValue)
            e.Handled = True
        End If

    End Sub

    Public Sub FuncKeysModule(ByVal value As Keys)


        'Check what function key is in a pressed state, and then perform the corresponding action.
        Select Case value
            Case Keys.F5
                Dim searchtype As Integer
                If txbPartNo.Focused = True Then
                    Popup_PartNumbers.Label1.Text = "Enter the beginning characters of the Part Number to search"
                    searchtype = 1
                    txbSearchType.Text = searchtype
                End If
                If txbDesc.Focused = True Then
                    Popup_PartNumbers.Label1.Text = "Enter a word from the Part Number Description to search"
                    searchtype = 2
                    txbSearchType.Text = searchtype
                End If
                Popup_PartNumbers.ShowDialog()
        End Select

    End Sub

As it stands right now, 'F5' doesn't bring up the Popup_PartNumbers form.

So, several questions:

1) Can I use this one function key ('F5') for both routines (albeit I'm trying to choose one or the other)?
2) Am I using the correct event (GotFocus) to fire this of?
3) Is the code "If txbDesc.Focused = True Then" being correctly used (see above)?
4) If I'm using the wrong events, can you suggest what would be better to use?

In advance, thanks for your help.

Don

You might have more success using the KeyPress event

Is this what you had in mind?

    Public Sub txbPartNo_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txbPartNo.KeyPress
        If e.KeyValue = Keys.F5 Then
            FuncKeysModule(e.KeyValue)
            e.Handled = True
        End If

    End Sub

    Public Sub txbDesc_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txbDesc.KeyPress
        If e.KeyValue = Keys.F5 Then
            FuncKeysModule(e.KeyValue)
            e.Handled = True
        End If

    End Sub

I've gotten an error when trying to do this.

Error 1 Method 'Public Sub txbPartNo_KeyPress1(sender As Object, e As System.Windows.Forms.KeyEventArgs)' cannot handle event 'Public Event KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs)' because they do not have a compatible signature.

I'm thinking I don't clearly understand your instructions.

Don

try loading the stub from the events dropdown box. At the top of the code window, in the left hand dropdown box choose the textbox. Now in the right hand dropdown box choose KeyPress. that will automatically write the code stub for the event handler and you can add your code into it.

Tinstaafl,

Creating the event as you suggested works. However when I insert the event itself, I get the following message:

"Error 1 'KeyValue' is not a member of 'System.Windows.Forms.KeyPressEventArgs'."

The code looks like this:

Private Sub txbPartNo_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles txbPartNo.KeyPress
        If e.KeyValue = Keys.F5 Then
            FuncKeysModule(e.KeyValue)
            e.Handled = True
        End If
    End Sub

Any other suggestions?

Don

looks like it won't work with the keypress use the keydown instead. It will work with that.

Ok.... Thanks. That appears to be working.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.