I have a form that opens with a panel and multiple textboxes displayed that I created via the GUI. Once the user enters the desired information within this panel, a button is click and a new panel appears with the same textboxes displayed as the original. This panel and all of it's textboxes were created via code. As with the original, a button is clicked and another panel appears (identical to the first two). This third panel is created via the same code as the second.

Now the challenge: The original panel has one textbox that has a Sub FuncKeysModule attached to it that allows the user to use the function Key F1 perform a routine. The code looks like this:

Public Sub txbUM_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txbUM.KeyDown
        If e.KeyValue = Keys.F1 Then
            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.F1
                'Add the code for the function key F1 here.
                MessageBox.Show("F1 pressed")
        End Select

    End Sub

The specific textbox is called "txbUM". On the original panel, the F1 key works as it was designed. However when the subsequent panels are created via code, the function key no longer works, even though the textbox name is still "txbUM". The only difference is that this textbox is now within a different panel with a different name.

The question: Would you know how to ensure the Function Key F1 will work in the newly created panel(s)?

To further clarify, the form has a main, primary panel that all of this resides in. That main panel is called "pnlMainPanel". This main panel was designed on the form via the GUI. The smaller panel with the textboxes in it is called "pnlOriginalPanel". Again, this was designed on the form via the GUI. With the button click event, an identical panel is created and is now called "pnlOrderLine" (I hope I haven't confused everyone).

If you have questions, please don't hesitate to ask.

Thanks for your help.


5 Years
Discussion Span
Last Post by doncwilson_1

If you create and add controls through code then those controls will not have any events tied to them.
When you open the second and third panels and create controls in them, you also have to use the AddHandler statement and tie that to the txbUM_KeyDown event, or use a generic event and tie all three textboxes to it using AddHandler.

'For panel 2
AddHandler txbUM2.KeyDown, AddressOf txbUM_KeyDown

'For panel 3
AddHandler txbUM3.KeyDown, AddressOf txbUM_KeyDown

Like that.
And in the event itself you can use sender to identify which textbox triggered the event by using CType.

Dim tbox As TextBox = CType(sender, TextBox)


The problem with this is that I don't know how many new panels will be created by the end user. It could be 1 or 10 or 50. Learning how to do this will solves some other problems for me as well. Help!!



Call off the dogs. I added "AddHandler txbUM.KeyDown, AddressOf txbUM_KeyDown" to the click event that creates the new panel. It worked and did what I needed it to do.

Thanks for the idea!


This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.