How to apply keydown handle to all controls in a form or a container?

Hi, I am creating textboxes at runtime, but I need to handle the function keys in all of them.

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

Dim a As Integer

For a = 0 To 10

Me.Controls.Add(New Windows.Forms.TextBox)

Me.Controls.Item(a).Top = a * 20

Next a

End Sub

Private Sub Form1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown

Dim h

h = e.KeyValue

If h = 118 Then Beep()

End Sub



Answer this question

How to apply keydown handle to all controls in a form or a container?

  • Jeffrey Palermo

    For a = 0 To 10

    dim txt as new Windows.Forms.TextBox

    txt.Top = a * 20

    Me.Controls.Add(txt)

    Addhandler txt.KeyUp, addressof Form1_KeyUP

    Next a



  • XCode247

    Hello. You need to just use AddHandler to assign it to an event handler.

     


    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Dim a As Integer

    Dim newTextBox As Windows.Forms.TextBox
    For a = 0 To 10
    newTextBox =
    New Windows.Forms.TextBox
    Me.Controls.Add(newTextBox)
    AddHandler newTextBox.KeyUp, AddressOf GenericKeyUpHandler
    Me.Controls.Item(a).Top = a * 20
    Next a
    End Sub

    Private Sub GenericKeyUpHandler(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs)

    Dim h
    h = e.KeyValue
    If h = 118 Then Beep()

    End Sub

     

     



  • How to apply keydown handle to all controls in a form or a container?