473,387 Members | 1,700 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Unwanted KeyUp events from child dialogs

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
2 2337
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: CJack | last post by:
hy, I have an mdi application, i create a child form and I want to know when a button is pressed while that child form is loaded. I have this code: private void frmTestBaby_KeyUp(object...
3
by: Sharon | last post by:
I’m using a DataGrid control on my form; and I want to handle the KeyUp event. So I did: myDataGrid.KeyUp += new KeyEventHandler(this.OnKeyUp); When the DataGrid is empty, I mean when It has...
2
by: ZS | last post by:
Hi, On a form , I'm trying to trap when a shift key is pressed. Can someone explain how the KeyUp,KeyDown and Key Press event works for Forms. Thanks -ZS
4
by: Serdge Kooleman | last post by:
How to get "Key press" event when i'm working with DataGrid (winforms). Seems to me that standard events "KeyUp, KeyPress" are ignored :-( Thank you
4
by: **Developer** | last post by:
I have a usercontrol that contains the following. To my surprise the form containing this control get KeyUp events. Help says that for KeyPress setting e.Handled = True suppresses KeyPress...
4
by: ShaneO | last post by:
I would like to handle the KeyUp & KeyDown events in the same event handler but can't find how to determine which event was fired - Private Sub ListBox1_KeyUp(ByVal sender As Object, ByVal e As...
11
by: Ben | last post by:
Hello, I'm trying to catch the pressing of the left and right arrow keys through the KeyUp event, like so: public Form1() { InitializeComponent(); Form1.ActiveForm.KeyUp += new...
5
by: sklett | last post by:
I know that Panel (and most of it's derivitives) don't raise keyboard events. I *really* need to catch keyboard events though so I've been googling the topic and have found quite a few...
2
by: Tony Johansson | last post by:
Hello! I have created a Control that consist of a label and a textbox.I have called this class ctlLabelTextbox. public partial class ctlLabelTextbox : UserControl { .... } The class that I...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.