Unwanted KeyUp events from child dialogs

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.
 
G

Guest

I suspect that in Form2 the form is closed when the return key is pressed
(Keydown event) then when you close it, the only form left to accept the
KeyUp event is Form1. Strange but you might set Form1 KeyPreview to False
before you show Form2 then reset it if you want after the dialog is returned.
 
A

Adam J. Schaff

There are ways to deal with it from Form1, I am certain. But the real issue
is that if I write a reusable dialog, I would like it to be "polite" and not
drop leftover KeyUp events to forms that call it. Therefore, I would like to
handle the KeyUp in Form2 somehow. I'm sure there must be a way.
 
G

Guest

You might set a global Form2 flag initialized in the Activate event to False
then in the button click event set it to True. This would allow checking the
return key in the keyup event and closing the form if the global flag is
True. This is a bit of a kludge but should work.
 
D

DevNick

Hmm odd, but what I would do is put a keyup/keypress handler on Form2

Then test for what they pressed(enter for instance) but I can't remember if
you can do keypreview or whatever its called now on Dialogues.
Anyway.. i would try useing a (keypress event) = e.Keyhandled = True and on
keydown and keyup do the same. adding in the e.Keyhandled = True. That
should stop it from being passed on.

However when you double clicked the form1 and caused form2 to load you may
want to force focus to form2 just to be safe and then on form1 you can also
put in a handler to ignore the carriage return. Or use a delegate for both..
and just have one handler..
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top