473,587 Members | 2,510 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(ByV al sender As Object, ByVal e As
System.Windows. Forms.KeyEventA rgs) Handles MyBase.KeyUp
If e.KeyCode = Keys.Return Then
MsgBox("oops!")
End If
End Sub

Private Sub Form1_DoubleCli ck(ByVal sender As Object, ByVal e As
System.EventArg s) Handles MyBase.DoubleCl ick
Dim frm As New Form2
frm.ShowDialog( )
End Sub

Form2 has a single button on it and this code underneath:

Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As
System.EventArg s) 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 2347
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****@discuss ions.microsoft. com> wrote in message
news:6F******** *************** ***********@mic rosoft.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(ByV al sender As Object, ByVal e As
System.Windows. Forms.KeyEventA rgs) Handles MyBase.KeyUp
If e.KeyCode = Keys.Return Then
MsgBox("oops!")
End If
End Sub

Private Sub Form1_DoubleCli ck(ByVal sender As Object, ByVal e As
System.EventArg s) Handles MyBase.DoubleCl ick
Dim frm As New Form2
frm.ShowDialog( )
End Sub

Form2 has a single button on it and this code underneath:

Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As
System.EventArg s) 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*****@cascod ev.com> wrote in message
news:%2******** ********@TK2MSF TNGP12.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(ByV al sender As Object, ByVal e As
System.Windows. Forms.KeyEventA rgs) Handles MyBase.KeyUp
If e.KeyCode = Keys.Return Then
MsgBox("oops!")
End If
End Sub

Private Sub Form1_DoubleCli ck(ByVal sender As Object, ByVal e As
System.EventArg s) Handles MyBase.DoubleCl ick
Dim frm As New Form2
frm.ShowDialog( )
End Sub

Form2 has a single button on it and this code underneath:

Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As
System.EventArg s) 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
2072
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 sender, System.EventArgs e) { MessageBox.Show("keyboard button pressed!");
3
3114
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 no data so now rows are seen, all keys are causing my this.OnKeyUp(...) to be invoked correctly. But when the DataGrid is populated with some data...
2
4091
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
8491
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
6962
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 events but say very little about KeyUp. What does setting to Handled do?
4
7124
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 System.Windows.Forms.KeyEventArgs) _ Handles ListBox1.KeyUp, ListBox1.KeyDown If e.KeyValue = Keys.PageDown Or e.KeyValue = Keys.PageUp Or...
11
5649
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 KeyEventHandler(ActiveForm_KeyUp);
5
6100
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 suggestions. The one suggestion I've found that makes the most sense isn't working for me and after this most recent failure I'm really at a loss! ;0) I'm...
2
19284
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 have created for this purpose is derived from class UserControl.
0
7923
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7852
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8216
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8221
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6629
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5395
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3845
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2364
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1192
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.