My application has two forms.
Focus may be on either form.
Keypresses on either Form1 or on Form2 need to processed by code in Form1.vb.
How should I make this happen?
Thank you
retroMIDI2
My application has two forms.
Focus may be on either form.
Keypresses on either Form1 or on Form2 need to processed by code in Form1.vb.
How should I make this happen?
Thank you
retroMIDI2
Here's one way:
Public Class Form1
Public Sub Form1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
Dim CurrentForm As Form = DirectCast(sender, Form)
MsgBox(e.KeyChar.ToString + " " + CurrentForm.Name)
End Sub
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
AddHandler Me.KeyPress, AddressOf Form1_KeyPress
Form2.Show()
End Sub
End Class
Public Class Form2
Private Sub Form2_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
AddHandler Me.KeyPress, AddressOf Form1.Form1_KeyPress
End Sub
End Class
Set the KeyPreview
property to true on each form. When you run this whichever form has the focus will echo the key pressed and the form name to the messagebox.
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.