A
Adam J. Schaff
I have recently noticed an unwanted behavior that I do not know how to get
rid of.
To Recreate Problem:
Windows Forms App with 2 forms. Form 1 has nothing on it and this code
underneath:
Private Sub Form1_KeyUp(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyUp
If e.KeyCode = Keys.Return Then
MsgBox("oops!")
End If
End Sub
Private Sub Form1_DoubleClick(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.DoubleClick
Dim frm As New Form2
frm.ShowDialog()
End Sub
Form2 has a single button on it and this code underneath:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Me.Close()
End Sub
Expected:
Run App, double-click anywhere on Form1. Form2 loads with focus on the
button. Press Enter. Form2 closes.
Actual:
Same as Expected except that KeyUp fires on Form1 and I get the "oops!" msg.
What is happening is that when I click ENTER on Form2, it fires the button
click which closes Form2. However, it apparently does NOT consider the KeyUp
to have been handled, so it passes that along to Form1, causing the unwanted
side-effect.
Is there any way to prevent this from happening? In particular, I am looking
for code to put in the Button1_Click event of Form2 that will prevent KeyUp
from firing on Form1. Please help. This one has been bugging me for a
while.
rid of.
To Recreate Problem:
Windows Forms App with 2 forms. Form 1 has nothing on it and this code
underneath:
Private Sub Form1_KeyUp(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyUp
If e.KeyCode = Keys.Return Then
MsgBox("oops!")
End If
End Sub
Private Sub Form1_DoubleClick(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.DoubleClick
Dim frm As New Form2
frm.ShowDialog()
End Sub
Form2 has a single button on it and this code underneath:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Me.Close()
End Sub
Expected:
Run App, double-click anywhere on Form1. Form2 loads with focus on the
button. Press Enter. Form2 closes.
Actual:
Same as Expected except that KeyUp fires on Form1 and I get the "oops!" msg.
What is happening is that when I click ENTER on Form2, it fires the button
click which closes Form2. However, it apparently does NOT consider the KeyUp
to have been handled, so it passes that along to Form1, causing the unwanted
side-effect.
Is there any way to prevent this from happening? In particular, I am looking
for code to put in the Button1_Click event of Form2 that will prevent KeyUp
from firing on Form1. Please help. This one has been bugging me for a
while.