By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,081 Members | 861 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,081 IT Pros & Developers. It's quick & easy.

Unwanted KeyUp events from child dialogs

P: n/a
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.
Nov 21 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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.

"Adam J. Schaff" wrote:
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.

"Dennis" <De****@discussions.microsoft.com> wrote in message
news:6F**********************************@microsof t.com...
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.

"Adam J. Schaff" wrote:
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.


Nov 21 '05 #2

P: n/a
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..

"Adam J. Schaff" <as*****@cascodev.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
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.

Nov 21 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.