Refactor (extract) the code into a new method (Sub/Function). Then you can call it from multiple places in your code. Example:
BEFORE:
Public Class Form1
Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles MyBase.KeyDown
End Sub
End Class
AFTER:
Public Class Form1
Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles MyBase.KeyDown
NewMethod()
End Sub
Sub NewMethod()
End Sub
Sub OtherMethod()
NewMethod()
End Sub
End Class
Updated with new requirement: For the new requirement, you need to use
WithEvents
to capture the KeyDown event.Here is a new example to reflect this.
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
OpenForm()
End Sub
Private WithEvents picform As New Form
Sub OpenForm()
picform.Height = 800
picform.Width = 600
picform.Show()
End Sub
Private Sub picform_KeyDown(sender As Object, e As KeyEventArgs) Handles picform.KeyDown
MsgBox("Picformn keydown event caputred in parent form", MsgBoxStyle.Exclamation)
End Sub
End Class