First of all, none of the forms are children here, and your main form is not a parent of your other form.
If you call
System.Windows.Forms.Form.ShowDialog
, your form is shown in the
modal state, so the user is supposed to work only with this form in this application (so, it is not
system-modal, you can switch to other applications, activate their windows), cannot invoke events of other forms.
Please see:
https://msdn.microsoft.com/en-us/library/system.windows.forms.form.showdialog(v=vs.110).aspx[
^].
It is not clear what is it and why "Frm2 comes to MainForm_KeyUp". Here is how it look. You did provide any evidence
that MainForm_KeyUp
is an event handler added to the invocation list of the
KeyUp
event instance of any of the forms. This is just suggested by the name of the method, as it looks like an auto-generated code you got from the designer. It does not tell anything by itself; adding a delegate to an invocation list is done using += event operator. And using auto-generated names of methods permanently is against good programming style, as such names violate (good) Microsoft naming conventions.
There is another problem of handling keyboard events on the forms: you probably have some controls which grab keyboard focus, so the
KeyDown
event is not dispatched to the form itself. If you need assign true to the property
Form.KeyPreview
and handle the event
PreviewKeyDown
or override the method
OnPreviewKeyDown
:
https://msdn.microsoft.com/en-us/library/system.windows.forms.control.previewkeydown(v=vs.110).aspx[
^].
https://msdn.microsoft.com/en-us/library/system.windows.forms.control.previewkeydown(v=vs.110).aspx[
^],
https://msdn.microsoft.com/en-us/library/system.windows.forms.control.onpreviewkeydown(v=vs.110).aspx[
^].
I doubt that handling
KeyUp
and checking up Enter makes any practical sense at all.
The question is not clear enough; you would need to start with explaining the purpose of what you are doing; so, you can get more help if you further clarify the question.
—SA